View Full Version : Associative tables
Tipperton
05-28-2005, 10:25 AM
The help file says you can do this:food{};
food.veggie = "Carrot";
food.fruit = "Apple";
drink = food.fruit.." cider";but what if my index is in a variable? Like this:foodtype = "fruit";Is there a way to use that variable to access the appropriate table element?
Possibly like these?drink1 = food.foodtype.." cider";
drink2 = food[foodtype].." cider";Thanks!
Corey
05-28-2005, 02:05 PM
Hi Tipperton. Hope all is well, please don't replace your sig, the reason it got removed is that we wish to keep this forum extremely neutral and occasionally references, even literary ones, can be misunderstood by others. I hope you can appreciate our position. It's nothing to do with you personally, it's just the way we've defaulted to managing the forum. :)
Anyhow OK onto your question:
Is there a way to use that variable to access the appropriate table element?
Possibly like these?
Code:
drink1 = food.foodtype.." cider";
drink2 = food[foodtype].." cider";
Here's a couple ways to access elements of an associative table for example:
table={name="John", age="32"}
Dialog.Message("Name is: ", table["name"]);
Dialog.Message("Age is : ", table.age);
You could also do that using a variable to store your index value like this:
table={name="John", age="32"}
item="name";
Dialog.Message("Name is: ", table[item]);
Not sure if that answers your question but I hope it helps. :yes :)
Tipperton
05-28-2005, 02:36 PM
That explains how it got truncated to just the name (I was sure I put the whole thing in). Now even the name is gone... :wow
Yes that answers my question, thanks.
Corey
05-28-2005, 02:59 PM
Glad to help, feel free to put your name back, sorry for deleting it. :yes :)
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.