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.
Iterating through a table if it doesn't have an index?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Lemony Lime
- Prole
- Posts: 22
- Joined: Fri Dec 28, 2012 9:35 pm
Re: Iterating through a table if it doesn't have an index?
Code: Select all
for key,value in pairs(table) do
...
end
also, if you want to get at one of those objects, you'd do
Code: Select all
fruit.apples
Code: Select all
local fruitName = 'apples'
fruit[fruitName]
Re: Iterating through a table if it doesn't have an index?
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:
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
Code: Select all
indexes = {"apples", "oranges", "pears"}
Code: Select all
fruits.apples
Code: Select all
fruits[ indexes[1] ]
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
Re: Iterating through a table if it doesn't have an index?
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:
and access the table like this:
also I would recommend not to use index = 0, as its something not natural in lua, so the example should be:
witch also enables the numeric for iteration:
witch is the same as:
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'}
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
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
Code: Select all
for i=1, #fruit_id do
somevar = fruit[fruit_id[i]]
end
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?
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:
Alternatively, you could also do this:
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
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
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Who is online
Users browsing this forum: Ahrefs [Bot] and 6 guests