Iterating through a table if it doesn't have an index?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Lemony Lime
Prole
Posts: 22
Joined: Fri Dec 28, 2012 9:35 pm

Iterating through a table if it doesn't have an index?

Post 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.
User avatar
seanmd
Prole
Posts: 35
Joined: Sat Jun 22, 2013 7:47 am

Re: Iterating through a table if it doesn't have an index?

Post 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

Code: Select all

fruit.apples
or to make it something you can access programmatically

Code: Select all

local fruitName = 'apples'
fruit[fruitName]
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Iterating through a table if it doesn't have an index?

Post 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

Code: Select all

fruits.apples
but when you want to use a numerical index you'll also be able to write

Code: Select all

fruits[ indexes[1] ]
Wojak
Party member
Posts: 134
Joined: Tue Jan 24, 2012 7:15 pm

Re: Iterating through a table if it doesn't have an index?

Post 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
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Iterating through a table if it doesn't have an index?

Post 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
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
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 6 guests