Help with Tables

Posted by Deladan on Tue 24 May 2011 08:41 PM — 7 posts, 22,362 views.

#0
Here goes, I'm trying to make a table that will track every person that I happen to enemy and then remove them when I unenemy them. The triggers would be

^(\w+) is now one of your enemies\.$

to add to the table

and

^You declare that (\w+) will no longer be one of your enemies\.$

^In a moment of forgiveness, you declare that you have no enemies\.$

to remove from the table

now what I'd like done with the table is to turn it into a varibable so that I can modify the trigger that Nick helped me with (see did for me basically) to look like this


<trigger
custom_colour="17"
enabled="y"
expand_variables="y"
group="Highlights"
ignore_case="y"
match="^With a sudden convulsion, (\w+) collapses helplessly to the ground\.$"
regexp="y"
send_to="12"
sequence="100"
other_text_colour="lime"
>
<send>if "@enemies" == "%1" or if "@target" == "%1" then
ColourNote("white", "green", "%1 is prone!!!")
end
</send>
</trigger>
#1
So after talking with some people the trigger can be done like this


<trigger
custom_colour="17"
enabled="y"
expand_variables="y"
group="Highlights"
ignore_case="y"
match="^With a sudden convulsion, (\w+) collapses helplessly to the ground\.$"
regexp="y"
send_to="12"
sequence="100"
other_text_colour="lime"
>
<send>if "@enemies" == "%1" or "@target" == "%1" then
ColourNote("white", "green", "%1 is prone!!!")
end
</send>
</trigger>

now I just wish I knew how to do a simple enough table to do what I want
#2
^(\w+) is now one of your enemies\.$

would you do

enemies = {} -- To create the table

but I don't know how to add to the table from a trigger
#3
^(\w+) is now one of your enemies\.$

would you do

enemies = {} -- To create the table

enemies ["%1"] = 0 -- puts the person in the table?


and from there I'm lost completely
#4
^(\w+) is now one of your enemies\.$

would you do

enemies = {} -- To create the table

enemies ["%1"] = 0 -- puts the person in the table?


-- sort enemies into alphabetic order
sorted_enemies = {}
for name in pairs (enemies) do table.insert (sorted_enemies, name) end
table.sort (sorted_enemies)


am I right so far?
#5
^(\w+) is now one of your enemies\.$

would you do

enemies = {} -- To create the table

enemies ["%1"] = 0 -- puts the person in the table?


-- sort enemies into alphabetic order
sorted_enemies = {}
for name in pairs (enemies) do table.insert (sorted_enemies, name) end
table.sort (sorted_enemies)

-- show enemies
if next (enemies) then
ColourNote ("red", "", "enemies now consists of " ..
table.concat (sorted_enemies, ", "))
end

I hope that's right
Australia Forum Administrator #6
Quote:

enemies = {} -- To create the table

enemies ["%1"] = 0 -- puts the person in the table?


You don't want to keep recreating the table, or it will only ever have one person in it. So do this instead:


enemies = enemies or {} -- To create the table


Or to have much the same effect:


-- if table does not exist, create it
if not enemies then
  enemies = {} -- To create the table
end -- if



Then:


enemies ["%1"] = true -- puts the person in the table


To remove that person:


enemies ["%1"] = nil -- removes the person in the table


You can print them in sorted order like this:


  require "pairsbykeys"
  for who in pairsByKeys (enemies) do
    print (who)
  end -- for