[solved] How to get a random item from this table/ or how to use an index in this table.

Hi,
I was reading about tables here:
http://www.gammon.com.au/forum/?id=6036

t = {

dwarf = { str = “22”, dex = “23”, wis = “18” },
human = { str = “20”, dex = “20”, wis = “20” },
elf = { str = “18”, dex = “24”, wis = “25” },

}

And am wondering if I print something from this table I use:
print(t.dwarf.str)

But what if I want to randomly print either strength dex or wisdom?
I thought I would use math.random on an index but I can’t manage to substitute it for an index in the normal way I do it when I only have one value in the table.
t={“a”,“b”,“c”}
print(t[2])

How would I do this?
print(t.dwarf[1]) or print(t.dwarf.[1]) gives a nil value.

[import]uid: 100901 topic_id: 19692 reply_id: 319692[/import]

its not pretty, but you can do something like that:
[lua]local t = {

dwarf = { str = “22”, dex = “23”, wis = “18” },
human = { str = “20”, dex = “20”, wis = “20” },
elf = { str = “18”, dex = “24”, wis = “25” },

}

math.randomseed(os.time())
local mRand = math.random(1,3)
local function ret()
if mRand == 1 then
return “str”
elseif mRand == 2 then
return “dex”
elseif mRand == 3 then
return “wis”
end
end

print(t[“dwarf”][ret()])[/lua] [import]uid: 16142 topic_id: 19692 reply_id: 76168[/import]

hmmm so something like:
print(t.[]1.[2])
or something similarly easy isn’t possible?
I actually get the random from shuffling the table. So I don’t know what the first entry is could be dwarf elf or human.
I am not using these entires but a list of questions with answers.
might need to think about a different way to do this.
Doing this for a lot of entries might nog be workable.

Thanks though. If I can’t come up with something easier, I might have to do this. [import]uid: 100901 topic_id: 19692 reply_id: 76176[/import]

Spinning off what @darkconsoles has done, you could also do something like this:

[lua]local creatures = {“dwarf”, “human”, “elf”};
local attributes = {“str”, “dex”, “wis”};

local t =
{
dwarf = { str = “22”, dex = “23”, wis = “18” },
human = { str = “20”, dex = “20”, wis = “20” },
elf = { str = “18”, dex = “24”, wis = “25” }
}

math.randomseed(os.time())

local cIndex = math.random(1, #creatures);
local aIndex = math.random(1, #attributes);

print(t[creatures[cIndex]][attributes[aIndex]]);[/lua] [import]uid: 70847 topic_id: 19692 reply_id: 76188[/import]

what build are you using
have u tried
print( t[dwarf][1] )

[import]uid: 7911 topic_id: 19692 reply_id: 76198[/import]

@jstrahan

No unfortunately that won’t work

t.dwarf.dex is shorthand notation for

t["dwarf"]["dex"]

t[dwarf][dex] will not work since ‘dwarf’ and ‘dex’ are parsed as variables

Also numerical indexing doesn’t work for tables created with non-numerical indexes.

[import]uid: 70847 topic_id: 19692 reply_id: 76200[/import]

Ok,
so if I would change my table from this:
vragenTable={
vraag1={
vraag=“foo”,
antwoord=“bar”
},
vraag2={
vraag=“foo”,
antwoord=“bar”
},
vraag3={
vraag=“foo”,
antwoord=“bar”
},
vraag4={
vraag=“foo”,
antwoord=“bar”
}
}
to
vragenTable={
1={
vraag=“foo”,
antwoord=“bar”
},
2={
vraag=“foo”,
antwoord=“bar”
},
3={
vraag=“foo”,
antwoord=“bar”
},
4={
vraag=“foo”,
antwoord=“bar”
}
}
it could work? Can’t test right now. [import]uid: 100901 topic_id: 19692 reply_id: 76205[/import]

you right I looked at sample in OP wrong
however since all contain same variables in same order he could restructure the table so it would use less code

t = {

dwarf = { “22”, “23”, “18” },
human = { “20”, “20”, “20” },
elf = { “18”, “24”, “25” },

}

then t.dwarf[1] would work

just an alturnitive suggestion [import]uid: 7911 topic_id: 19692 reply_id: 76206[/import]

Ok this works the way I can use it:

questionTable={}

questionTable[1]={
Question=“Aa”,
Answer=Aa
}
questionTable[2]={
Question=“Bb”,
Answer=Bb
}
questionTable[3]={
Question=“Cc”,
Answer=Cc
}
questionTable[4]={
Question=“Dd”,
Answer=Dd
}

print(questionTable[4].Answer)

Thanks for the help.
Changing the first bit to numerical made it work for me. So instead of having question, question2 etc. I now just use 1,2,3 etc.
A bit less readable but at least it works. [import]uid: 100901 topic_id: 19692 reply_id: 76211[/import]

You can also define the table in one go like:

[lua]questionTable = {
{ Question=“Aa”, Answer=“Aa” },
{ Question=“Bb”, Answer=“Bb” },
{ Question=“Cc”, Answer=“Cc” },
{ Question=“Dd”, Answer=“Dd” }
}

print(questionTable[4].Answer)[/lua] [import]uid: 70847 topic_id: 19692 reply_id: 76219[/import]

Thanks,
Still learning about Lua tables. i understood the basics of in Obj C. Switching to corona actually lets me try nested tables without being too scared.
Haven’t figured out yet which way of writing tables I prefer.
I am using corona project manager and the autoindent isn’t always working as expected(probably me). So am experimenting with different ways of making tables to keep readbillity at a max.
Sometimes I miss xcode. [import]uid: 100901 topic_id: 19692 reply_id: 76222[/import]

Btw thanks everyone for helping.
When I switched from obj to corona I was kind of afraid. The apple dev forums have a lot of really helpfull people. I didn’t know if Corona would have the same level of community support.

So far I haven’t been disappointed. Thanks! [import]uid: 100901 topic_id: 19692 reply_id: 76223[/import]