Page 1 of 1

Table Calling?

Posted: Fri Aug 12, 2011 6:02 am
by Hiriako
Hi all. I've defined the following table:
Code: [show] | [select all] lua
astronumeric ={"volcano","antlers","twincrystals","dolphin","lion","crocodile","burningcenser","spider","dragon","skull","bumblebee","glacier" }
This part I know to be functioning as when I call display(astronumeric) I get the following output:

table {
1: 'volcano'
2: 'antlers'
3: 'twincrystals'
4: 'dolphin'
5: 'lion'
6: 'crocodile'
7: 'burningcenser'
8: 'spider'
9: 'dragon'
10: 'skull'
11: 'bumblebee'
12: 'glacier'
}

That's exactly what I want, with 1 corresponding to volcano, etc...

So here's the issue. How do I actually manage to -call- an item from this table to use?

I was attempting to make an alias to access this table in the following format:

REGEX: ^as (\w+)$
Code: [show] | [select all] lua
send("astrocast " .. print(astronumeric[matches[2]]) .. " sphere at " .. target)
Target is a pre-defined variable that's functioning properly. I've tried a variety of things where I currently have print() with no results. Any thoughts?

Re: Table Calling?

Posted: Fri Aug 12, 2011 7:31 am
by keneanung
Just use
Code: [show] | [select all] lua
send("astrocast " .. astronumeric[matches[2]].. " sphere at " .. target)
That should fix it.

Re: Table Calling?

Posted: Fri Aug 12, 2011 7:43 am
by Hiriako
That's what I thought too, but it didn't do a thing. I tried with [] brackets as well as () brackets around matches[2].

Re: Table Calling?

Posted: Fri Aug 12, 2011 7:55 am
by keneanung
uh I forgot...
Code: [show] | [select all] lua
send("astrocast " .. astronumeric[tonumber(matches[2])].. " sphere at " .. target)
Matches are always strings, even though they should be numbers.

On another note, if you want to match only numbers after as, then your regex should be "^as (\d+)$".

\d matches only numbers, but \w matches numbers and characters.

Re: Table Calling?

Posted: Fri Aug 12, 2011 8:06 am
by Hiriako
Fantastic, it works now.

So if I need a number, just swap matches[2] with tonumber(matches[2]), which I presume is a function that converts the string into an integer.

Thank you very much. I appreciate it! This will help me greatly along the way, and now I understand a lot more about how to do things!

Re: Table Calling?

Posted: Sat Aug 13, 2011 4:28 pm
by Rakon
Be warned, that when you restart mudlet, the lua table will not be in the same order as it is now. If you need a table that remembers the order of what items are placed in, then look into phpTable function for lua. http://lua-users.org/wiki/MakingLuaLikePhp

Re: Table Calling?

Posted: Sat Aug 13, 2011 4:47 pm
by Hiriako
Interesting. The table's stayed in the same order to this point. I'm not sure why it's done that, or why it'd change in the first place. I've defined it using a script. Could that be keeping it stagnant?

Re: Table Calling?

Posted: Sun Aug 14, 2011 2:19 am
by Denarii
It will always be in the order as defined in scripts. The phpTable function makes non-numeric keys retain their order.