Table Calling?

Post Reply
Hiriako
Posts: 19
Joined: Thu Aug 11, 2011 5:14 pm

Table Calling?

Post 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?

User avatar
keneanung
Site Admin
Posts: 94
Joined: Mon Mar 21, 2011 9:36 am
Discord: keneanung#2803

Re: Table Calling?

Post by keneanung »

Just use
Code: [show] | [select all] lua
send("astrocast " .. astronumeric[matches[2]].. " sphere at " .. target)
That should fix it.

Hiriako
Posts: 19
Joined: Thu Aug 11, 2011 5:14 pm

Re: Table Calling?

Post 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].

User avatar
keneanung
Site Admin
Posts: 94
Joined: Mon Mar 21, 2011 9:36 am
Discord: keneanung#2803

Re: Table Calling?

Post 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.

Hiriako
Posts: 19
Joined: Thu Aug 11, 2011 5:14 pm

Re: Table Calling?

Post 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!

User avatar
Rakon
Posts: 350
Joined: Tue Feb 16, 2010 7:41 pm
Contact:

Re: Table Calling?

Post 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

Hiriako
Posts: 19
Joined: Thu Aug 11, 2011 5:14 pm

Re: Table Calling?

Post 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?

Denarii
Posts: 111
Joined: Thu Dec 03, 2009 10:54 pm

Re: Table Calling?

Post by Denarii »

It will always be in the order as defined in scripts. The phpTable function makes non-numeric keys retain their order.

Post Reply