modify value of a table

hi ojnab,

thanks but that don’t works by me…

local t = {     {2,    6},     {3,    6},     {4,    6},     {5,    6},     {1,    8},     {2,    8},     {3,    8},     {4,    8},     {5,    8}, } for i=1, #t do     t[i][1] = i end     for a=1, #t do     for k=1,#t[a] do         print(a,k) end end

the result is :

1 1 1 2 2 1 2 2 3 1 3 2 4 1 4 2 5 1 5 2 6 1 6 2 7 1 7 2 8 1 8 2 9 1 9 2 1 1 1 2 2 1 2 2 3 1 3 2 4 1 4 2 5 1 5 2 6 1 6 2 7 1 7 2 8 1 8 2 9 1 9 2

 

if you want this table

local t = {     {2,    6},     {3,    6},     {4,    6},     {5,    6},     {1,    8},     {2,    8},     {3,    8},     {4,    8},     {5,    8}, }

to turn into this table

local t = {     {1,    6},     {2,    6},     {3,    6},     {4,    6},     {5,    8},     {6,    8},     {7,    8},     {8,    8},     {9,    8}, }

then this piece of code will do it

for i=1, #t do     t[i][1] = i end

If you want to do something else then I guess you didn’t explain it properly… because I don’t understand what you are trying to do in your previous post.

hi ojnab,

sorry i test too faster and i made a mistake …your solution works thanks a lot.

Have a good night :wink:

Great! My pleasure to help.

re-hi,

i return to the forum because the code given by ojnab work very well but when i attempt to use it in my game i have an error.

i explain, i have a virgin table like this 

local p.disposition={ {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, }

next i have this function who give to me special value and modify the virgin table 

group.sortTable=function()     for a = 1, #group.choose do           for b=1, #group.choose[a] do            table.insert(p.disposition,group.choose[a][b].textQuantityChoose.count)          end     end end

and i attempt to have a table like this :

local p.disposition = {     {2,    6},     {3,    6},     {4,    6},     {5,    6},     {1,    8},     {2,    8},     {3,    8},     {4,    8},     {5,    8}, }

Next i would modify my table like ojnab explained and i do :

e.rearrange=function() for a=1, #p.disposition do p.disposition[a][1] = a end for i=1, #p.disposition do for j=1, #p.disposition[i] do paper[j]:setFrame( i ) end end end --sorry for the ident i have no my vim with me....

to attempt to have :

local p.disposition = {     {1,    6},     {2,    6},     {3,    6},     {4,    6},     {5,    8},     {6,    8},     {7,    8},     {8,    8},     {9,    8}, }

But i have an error on my table “attempt to reach a number”

I suppose that the problem is with my function group.sortTable=function() who modify the structure of the table…

but i don’t see how to format my table in an another way…

if I am unclear called me I try to explain better

Anybody ?

@espace3d 

I would like to help you but it is impossible to guess what you are trying to do from the info you provided in this thread.

So you need to provide some more context if somebody should be able to help you.

What I can tell you is that this part of your code looks very odd.

for i=1, #p.disposition do for j=1, #p.disposition[i] do paper[j]:setFrame( i ) end end

My guess is that paper is a table with spritesheets and you are setting the frame of the individual spritesheets in the nested for loops???

Consider this… the j variable iterates from 1 to 2 because 2 is the length of each of the nested tables in p.disposition. 

So what the code basically ends up doing (after looping) is it sets the frame of paper[1] and paper[2] to the length of p.disposition.

Is that really what you want to do? It doesn’t make much sense to me.

hi ojnab thanks for answering my post.

in fact i believe that

for i=1, #p.disposition do for j=1, #p.disposition[i] do paper[j]:setFrame( i ) end end

make this with this table :

local p.disposition = {     {1,    6},     {2,    6},     {3,    6},     {4,    6},     {5,    8},     {6,    8},     {7,    8},     {8,    8},     {9,    8}, }

for each paper (from 1 to 9) make setframe (6 or 8 according to the number of j > example for 1 is 6 and for 7 is 8)

it’s what i’m searching to do.

paper[1]:setFrame(6)

paper[9]:setFrame(8)

Ok I get it. Instead of this

for i=1, #p.disposition do for j=1, #p.disposition[i] do paper[j]:setFrame( i ) end end

Try this

for i=1, #p.disposition do paper[p.disposition[i][1]]:setFrame(p.disposition[i][2]) end

ha yes thank you. I have only the time to test your solution. i have re-read the lua manual for table and now it’s a little more clear for me.

i have only one question. supposing i have this table :

local t = {6,6,6,2,2}

 and i want to modify my table to become the same but with decrement :

local t = {2,2,6,6,6}

Is it possible to do ? thank again for your advices

Use table.sort - https://docs.coronalabs.com/api/library/table/sort.html

local t = {6,6,6,2,2} table.sort(t, function(a,b) return a\<b end)