[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Getting variables out of a list of variables.
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Getting variables out of a list of variables.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Wuggly   USA  (112 posts)  [Biography] bio
Date Sat 30 Jan 2016 09:04 PM (UTC)

Amended on Sat 30 Jan 2016 10:13 PM (UTC) by Wuggly

Message
I have a list of variables that were made using pairs, where k is the name and v is the value.

My question is how can I pull the first variable out of v? (and second, third, etc..)

I know this won't work, but to make things a bit more clear.

GetVariable("v[1]")
GetVariable("v[2]")
etc. etc..

The more I try to figure this out, the more confused I become. I did read through many of the help files here, but once I read so much, it becomes a jumbled mess in my head.

I should also note this is all in a plugin, so there aren't any other variables in the list.
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Sun 31 Jan 2016 12:44 AM (UTC)
Message
Quote:

I have a list of variables that were made using pairs, where k is the name and v is the value.


Please post this list of variables (or part of it) so the answer is in some sort of context.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Wuggly   USA  (112 posts)  [Biography] bio
Date Reply #2 on Sun 31 Jan 2016 02:19 AM (UTC)
Message

dex 8
da 1
con 14
luc 12
int 7
svs -4
ma 20
wis 9
dmg 30
hr 25
chr 1
str 10
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Sun 31 Jan 2016 09:16 PM (UTC)
Message
Do you mean like this?


stats = {
        dex = 8,
        da = 1,
        con = 14,
        luc = 12,
        int = 7,
        svs = -4,
        ma = 20,
        wis = 9,
        dmg = 30,
        hr = 25,
        chr = 1,
        str = 10,
}

for k, v in pairs (stats) do
  print (k)  -- key
  print (v)  -- value
  print ("") -- blank line
end -- for


In that example the key is the attribute, and the value is the amount (eg. int = 7)

Output from the above test:


luc
12

chr
1

hr
25

ma
20

wis
9

con
14

int
7

svs
-4

dex
8

str
10

dmg
30

da
1


Why do you want the "first" one? Don't you just want to know that attribute has what value?

If you just want to know what the "dmg" stat is, you can do this:


print (stats.dmg)  -- prints 30

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Wuggly   USA  (112 posts)  [Biography] bio
Date Reply #4 on Sun 31 Jan 2016 09:52 PM (UTC)
Message
What I basically did was copy the code from
http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=4176

To total up the stats. Here is the code below so you can see how I've modified it so far. The goal is to put these stats into a stats miniwindow I have created.


<triggers>
 
  <trigger
   enabled="n"
   expand_variables="y"
   keep_evaluating="y"
   match="(int:|wis:|dex:|str:|chr:|con:|luc:|dmg:|hr:|da:|ma:|svs:)(\D*\d+)"
   name="additup"
   regexp="y"
   repeat="y"
   script="attrib_trigger_script"
   send_to="12"
   sequence="100"
  >
  </trigger>

  <trigger
   enabled="n"
   match="You are using:"
   name="clearoldstats"
   script="start_of_equip_list"
   sequence="100"
  >
  </trigger>

</triggers>

<aliases>

  <alias
   match="update eq stats"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>EnableTrigger("clearoldstats", true)
EnableTrigger("additup", true)
Send "eq"
DoAfterSpecial (2, 'EnableTriggerGroup ("clearoldstats", false)', sendto.script)
DoAfterSpecial (2, 'EnableTriggerGroup ("additup", false)', sendto.script)
</send>
  </alias>
        
</aliases>

<script>
<![CDATA[

function attrib_trigger_script (name, line, wildcards)
  
  -- function to handle individual matches
  local function one_attrib (m, t)
    local name = "attrib_" .. string.gsub (t [1], ":", "")
    local value = tonumber (t [2])
    SetVariable (name, tonumber (GetVariable (name) or 0) + value)
  end -- one_herb 

  local re = rex.new (GetTriggerInfo (name, 1))  -- get the match text, make into a regexp
  re:gmatch (line, one_attrib)  -- match repeatedly, call function
  
end -- attrib_trigger_script  

function start_of_equip_list (name, line, wildcards)

  for k, v in pairs (GetVariableList()) do 
    if string.match (k, "^attrib_") then
      DeleteVariable (k)
    end -- one of the attrib_* variables
  end

end -- start_of_equip_list
]]>
</script>


While testing to print out the stat list, I used this.


<aliases>

  <alias
   script="show_equip_list"
   match="show attribs"
   enabled="y"
   sequence="100"
  >
  </alias>

and for the function

function show_equip_list (name, line, wildcards)

  for k, v in pairs (GetVariableList()) do 
    if string.match (k, "^attrib_") then
      k = string.gsub (k, "^attrib_dex", "dex")
      k = string.gsub (k, "^attrib_da", "da")
      k = string.gsub (k, "^attrib_con", "con")
      k = string.gsub (k, "^attrib_luc", "luc")
      k = string.gsub (k, "^attrib_ma", "ma")
      k = string.gsub (k, "^attrib_int", "int")
      k = string.gsub (k, "^attrib_svs", "svs")
      k = string.gsub (k, "^attrib_dmg", "dmg")
      k = string.gsub (k, "^attrib_wis", "wis")
      k = string.gsub (k, "^attrib_hr", "hr")
      k = string.gsub (k, "^attrib_chr", "chr")
      k = string.gsub (k, "^attrib_str", "str")
      print (k, v)
    end -- one of the attrib_* variables
  end

end -- show_equip_list


So what I mainly need is a way to pull out just the values so I can put them into the stats miniwindow, which is already labeled with the names, just not the values.

Hope this clears things up?

See how it gets the entire variable list. Maybe if I weren't so confused I could use your reply and somehow modify it to work like that so I can pull the values out for the stats miniwindow.
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Sun 31 Jan 2016 10:33 PM (UTC)

Amended on Sun 31 Jan 2016 10:37 PM (UTC) by Nick Gammon

Message
I think you are better off using script variables rather than client variables because they are easier to work with. However this would pull out the variables and their values:


 for k, v in pairs (GetVariableList()) do 
    name = string.match (k, "^attrib_(%a+)$")
    if name then
      value = tonumber (v)
      print (name, value)
    end -- if
  end


You don't need to test for each one - let the regexp do that for you.




You could turn them into a Lua table like this:


stats = { } 

for k, v in pairs (GetVariableList()) do 
    name = string.match (k, "^attrib_(%a+)$")
    if name then
      value = tonumber (v)
      stats [name] = value
    end -- if
  end

require "tprint"

tprint (stats)


Output:


"dex"=8
"con"=14
"luc"=12
"da"=1

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Wuggly   USA  (112 posts)  [Biography] bio
Date Reply #6 on Sun 31 Jan 2016 11:25 PM (UTC)

Amended on Mon 01 Feb 2016 12:47 AM (UTC) by Wuggly

Message
Thank you very much Nick! I placed them into a table like you demonstrated and have successfully put them into my miniwindow!

Extremely grateful!
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


2,632 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]