Tables as Lists

Post Reply
Felix
Posts: 8
Joined: Fri Mar 29, 2013 3:58 pm

Tables as Lists

Post by Felix »

Hello. I'm a nonprogrammer trying to understand Mudlet's treatment of MyEnemies. It says to declare the variable MyEnemies as

MyEnemies = {}

My first question is about where to declare this. My first thought was in "Scripts", but so far this hasn't worked for me.
I have a parallel question about calling the Lua function "table.insert( listName, item )".

table.insert( myEnemies, "Tom" )
table.insert( myEnemies, "Jim" )

Where do I do this? (In "Aliases", or what?) I'm guessing "Aliases", because you're told to set up a pattern, "^add (.*) enemy"

A final question. How do you display the list using

Display { MyEnemies }

I guess I'm asking how to run a script.

Thank you for helping my with these basic questions.

Felix

User avatar
Vadi
Posts: 5050
Joined: Sat Mar 14, 2009 3:13 pm

Re: Tables as Lists

Post by Vadi »

Hi - yeah, you do it in scripts. Make a new one and put MyEnemies = {} in. It will create/reset the table whenever the script is run (on Mudlet load and when you save it).

Yes, you'd use table.insert whenever you'd like to insert. So in an alias, since matches[2] would be the name, you would use table.insert(MyEnemies, matches[2]). Note the same capitalization of MyEnemies - it matters.

display(MyEnemies) will display it.

Felix
Posts: 8
Joined: Fri Mar 29, 2013 3:58 pm

Re: Tables as Lists

Post by Felix »

Thank you very much, Vadi.

Post Reply