Associative tables

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Tipperton
    Forum Member
    • Oct 2003
    • 26

    Associative tables

    The help file says you can do this:
    Code:
    food{};
    food.veggie = "Carrot";
    food.fruit = "Apple";
    drink = food.fruit.." cider";
    but what if my index is in a variable? Like this:
    Code:
    foodtype = "fruit";
    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";
    Thanks!
    Last edited by Tipperton; 05-28-2005, 09:27 AM.
  • Corey
    Indigo Rose Staff Alumni
    • Aug 2002
    • 9745

    #2
    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:

    Code:
    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:

    Code:
    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

    Comment

    • Tipperton
      Forum Member
      • Oct 2003
      • 26

      #3
      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...

      Yes that answers my question, thanks.

      Comment

      • Corey
        Indigo Rose Staff Alumni
        • Aug 2002
        • 9745

        #4
        Glad to help, feel free to put your name back, sorry for deleting it. :yes

        Comment

        Working...
        X