Adding/editing a new array key/value from a trigger

Posted by Batista on Wed 23 Nov 2005 11:11 PM — 2 posts, 14,790 views.

#0
Greetings,

I've been messing around with arrays for a little bit and wanted to create a little script that would hold the value of slain things. However, when I try to do it I get one of those "You're trying to add a number to a nil value, you dummy!" messages.

More or less, I'm trying for something like this:

Trigger: You have slain thingeh.

A script will look for an entry called "thingeh" in the array called "slain". If it is there already, the script will add 1 to it. If it isn't, the script will add a new entry to the array called "thingeh" and set the value to the number 0 (not a string).

I've been at this for hours. Help?
Australia Forum Administrator #1
This is easy enough. The trigger below will do that. I haven't addressed saving the table between sessions. There is a forum post about that:


http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=4960


This discusses how you would save the table when MUSHclient saves its world, and reload it when you start it up again.

The trigger is:


<triggers>
  <trigger
   enabled="y"
   match="You have slain *"
   send_to="12"
   sequence="100"
  >
  <send>

if not slain then
  slain = {}  -- create table if necessary
end -- if

if slain ["%1"] then
  slain ["%1"] = slain ["%1"] + 1
else
  slain ["%1"] = 1  -- first one of these
end -- if

Note ("Now killed ", slain ["%1"], " %1")
</send>
  </trigger>
</triggers>



It creates the table if it doesn't exist. Then it looks for the item by key, and if it exists, adds 1 to it (ie. the count). Otherwise it sets it to 1, as it is the first time it is slain.