Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Question about inserting into tables

edited August 2012 in Questions Posts: 71

First, a code snippet:

for h = 1, numOfPlayers do
        homeworld = {}
        homeworld1 = {}
        s = string.format("homeworld" .. h)
        homeworld = HomeworldGeneration(player1Race, h)
        homeworld1 = { s=homeworld }
        table.insert(cluster, homeworld1)
    end

What I'm trying to do is insert the actual string in s to be the key for the homeworld table. I've tried to do just

homeworld1 = { "homeworld" .. h=homeworld }

which errors out, and the s version in the initial example. I can't seem to get the key to be anything other than with the literal value, or it errors out and refuses to run. I've looked through the Codea reference, online docs, and I can't seem to find an answer. Basically, there will be anywhere from 1-4 homeworlds and I need unique inserts into the table so I can sort through it faster.

Any help is appreciated!

Comments

  • edited August 2012 Posts: 71

    Never mind. Answered my own question. I need to get a rubber duck to sit next to my iPad.

    The answer follows:

    homeworld1["homeworld" .. h] = homeworld
    table.insert(cluster, homeworld1)
    

    Inserts the right variable into the table, so the output looks like:

    [{"homeworld1":{"systemID":"homeworld1","systemName":"Sol","systemSize":"normal","player1Explored":false,"systemType":"single","coord_x":0,"player3Explored":false,"coord_y":0,"wormhole":[4,13,10],"player4Explored":false,"player2Explored":false,"systemColor":"yellow"}},
    

    Instead of:

    [{"systemID":"homeworld1","systemName":"Sol","systemSize":"normal","player1Explored":false,"systemType":"single","coord_x":0,"player3Explored":false,"coord_y":0,"wormhole":[4,13,10],"player4Explored":false,"player2Explored":false,"systemColor":"yellow"}},
    

    which means I can check for the upper level "homeworld1" rather than search the whole file for "systemID":"homeworld1"

Sign In or Register to comment.