Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to output pairs() in order ? — Gideros Forum

How to output pairs() in order ?

twisttaptwisttap Member
edited December 2012 in General questions
Hello,

I have a multi dimensional tables like below and when I use a for loop to output them with pairs the output I get is not the way I like. Not in a proper order. Is there a way to get a proper output for this ?

TABLES:
recipes = {
type1 = {
	food1 = {name="Food 1", person=4, time=45, calorie=755, id=1},
	food2 = {name="Food 2", person=4, time=45, calorie=755, id=2},
	food3 = {name="Food 3", person=4, time=45, calorie=755, id=3},
	food4 = {name="Food 4", person=4, time=45, calorie=755, id=4},
	food5 = {name="Food 5", person=4, time=45, calorie=755, id=5},
	food6 = {name="Food 6", person=4, time=45, calorie=755, id=6}
},
type2 = {
	food1 = {name="Food 7", person=4, time=45, calorie=755, id=1},
	food2 = {name="Food 8", person=4, time=45, calorie=755, id=2},
	food3 = {name="Food 9", person=4, time=45, calorie=755, id=3},
	food4 = {name="Food 10", person=4, time=45, calorie=755, id=4},
	food5 = {name="Food 11", person=4, time=45, calorie=755, id=5},
	food6 = {name="Food 12", person=4, time=45, calorie=755, id=6}
}
}
CODE TO OUTPUT:
for k,v in pairs(recipes) do
		for i,j in pairs(v) do
print(j.name, j.person, j.calorie, j.time, j.id)
		end	
 
 
	end
-- Print Output below --
1
Food 1	4	755	45	1
Food 6	4	755	45	6
Food 3	4	755	45	3
Food 2	4	755	45	2
Food 5	4	755	45	5
Food 4	4	755	45	4
2
Food 7	4	755	45	1
Food 12	4	755	45	6
Food 9	4	755	45	3
Food 8	4	755	45	2
Food 11	4	755	45	5
Food 10	4	755	45	4

Comments

  • atilimatilim Maintainer
    edited December 2012 Accepted Answer
    You can use arrays:
    recipes = {
    {
    	{name="Food 1", person=4, time=45, calorie=755, id=1},
    	{name="Food 2", person=4, time=45, calorie=755, id=2},
    	{name="Food 3", person=4, time=45, calorie=755, id=3},
    	{name="Food 4", person=4, time=45, calorie=755, id=4},
    	{name="Food 5", person=4, time=45, calorie=755, id=5},
    	{name="Food 6", person=4, time=45, calorie=755, id=6}
    },
    {
    	{name="Food 7", person=4, time=45, calorie=755, id=1},
    	{name="Food 8", person=4, time=45, calorie=755, id=2},
    	{name="Food 9", person=4, time=45, calorie=755, id=3},
    	{name="Food 10", person=4, time=45, calorie=755, id=4},
    	{name="Food 11", person=4, time=45, calorie=755, id=5},
    	{name="Food 12", person=4, time=45, calorie=755, id=6}
    }
    }
    and ipairs instead of pairs:
    for k,v in ipairs(recipes) do
    	for i,j in ipairs(v) do
    		print(j.name, j.person, j.calorie, j.time, j.id)
    	end	
    end
    note: btw, using the length operator (#) is more efficient than ipairs
  • Completely agree to what @atilim says.

    BTW in javascript there is a standard optimization where you cache length in a variable and reuse it and not getting length on every iteration of the loop. Does the same apply to lua?
    Is there a difference in performance between:
    for i = 1, i < #obj do
     
    end
    and
    local l = #obj
    for i = 1, i < l do
     
    end
    ?
  • atilimatilim Maintainer
    edited December 2012
    Reference manual http://www.lua.org/manual/5.1/manual.html#2.4.5 says that: All three control expressions are evaluated only once, before the loop starts.

    It seems they are same in Lua.

    Likes: ar2rsawseen

    +1 -1 (+1 / -0 )Share on Facebook
  • Thanks for replies, using an array did the work for me.
Sign In or Register to comment.