Page 1 of 1
Iterating through a table if it doesn't have an index?
Posted: Tue Jul 02, 2013 10:48 pm
by Lemony Lime
So, I have a table something along these lines:
fruit = {
apples = { 'a', "red", 5 }
oranges = { 'o', "orange", 12 }
pears = { 'p', "green", 7 }
}
It doesn't seem like it's possible to access them based on their index, and the values themselves are tables, so I just made the first value of the nested table the index of it, so it now looks like this:
fruit = {
apples = { 0, 'a', "red", 5 }
oranges = { 1, 'o', "orange", 12 }
pears = { 2, 'p', "green", 7 }
}
So, now any time I use one of these tables, I know what the index is, but still can't get to the table using the index, so I started to write a function that loops through them all, and check the indexes until it finds the right one. Then I realized... how can I loop through them if I can't already refer to them by their index? So, now I'm stuck. I really want to be able to type fruit.apples vs fruit[1] most of the time, but of course it's necessary to do both at times.
Re: Iterating through a table if it doesn't have an index?
Posted: Tue Jul 02, 2013 10:52 pm
by seanmd
Code: Select all
for key,value in pairs(table) do
...
end
it is significantly slower than ipairs, but not worth worrying about.
also, if you want to get at one of those objects, you'd do
or to make it something you can access programmatically
Code: Select all
local fruitName = 'apples'
fruit[fruitName]
Re: Iterating through a table if it doesn't have an index?
Posted: Wed Jul 03, 2013 4:49 am
by T-Bone
I don't really understand what your problem is. If you want numerical indexes, why don't you just use them? Alternatively, create an extra table like this:
Code: Select all
indexes = {"apples", "oranges", "pears"}
Now, when you want to use the string as index, you can still write
but when you want to use a numerical index you'll also be able to write
Re: Iterating through a table if it doesn't have an index?
Posted: Wed Jul 03, 2013 5:51 am
by Wojak
If I understand you correctly You want to access tables indexed by strings by using integers?
Well as long as apples will always be 0, oranges always 1 and pears always 2 you can use a helper table:
Code: Select all
fruit_id = {[0]='apples',[1]='oranges',[2]='pears'}
and access the table like this:
Code: Select all
somevar = fruit[fruit_id[0]] -- the same as somevar = fruit.apples
somevar = fruit[fruit_id[1]] -- the same as somevar = fruit.oranges
somevar = fruit[fruit_id[2]] -- the same as somevar = fruit.pears
also I would recommend not to use index = 0, as its something not natural in lua, so the example should be:
Code: Select all
fruit_id = {'apples','oranges','pears'}
somevar = fruit[fruit_id[1]] -- the same as somevar = fruit.apples
somevar = fruit[fruit_id[2]] -- the same as somevar = fruit.oranges
somevar = fruit[fruit_id[3]] -- the same as somevar = fruit.pears
witch also enables the numeric for iteration:
Code: Select all
for i=1, #fruit_id do
somevar = fruit[fruit_id[i]]
end
witch is the same as:
Code: Select all
for i,v in pairs(fruit) do
somevar = v
end
Re: Iterating through a table if it doesn't have an index?
Posted: Mon Jul 08, 2013 5:51 pm
by davisdude
Also: indexes in Lua start at 1 usually, not 0.
In any case, if I think I understand you correctly, you want something like this:
Code: Select all
fruits = {
apples = { first_letter = 'a', color = 'red', amount = 5 },
oranges = { first_letter = 'o', color = 'orange', amount = 12 },
pears = { first_letter = 'p', color = 'green', amount = 7 },
}
for i, fruit in pairs(fruits) do
print(fruit.color)
end
Alternatively, you could also do this:
Code: Select all
fruits = {
{ name = 'apples', first_letter = 'a', color = 'red', amount = 5 },
{ name = 'oranges', first_letter = 'o', color = 'orange', amount = 12 },
{ name = 'pears', first_letter = 'p', color = 'green', amount = 7 },
}
for i, fruit in ipairs(fruits) do
print(fruit.name..', '..fruit.first_letter..', '..fruit.color..', '..fruit.amount)
end