Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Merge/append "tables" in lua gideros? — Gideros Forum

Merge/append "tables" in lua gideros?

piepie Member
edited March 2013 in Code snippets
Hi,
I think that this one should be easy :P but I can't see it...

I need to append one table to another, on certain conditions.

I don't have my code with me now, so I'll try to write down the logic flow.
I have 2 tables t1 and t2:

t1 = {}
t1[i] = {A,B,C}


t1 is updated by user actions,
t2 is a copy of t1, and its values are processed by function processData(t2) until there are no more t2[i] and then t2 is set to {} by the same function.

after filling t2 (copying values from t1 to t2), t1 resets to empty and starts collecting values again.

t2 = t1
t1 = {}

There is a condition in which I need to append t1 values to t2, before emptying t1
(if t2 is not empty, since if there are no more t2[i] to process, processData() sets t2 = {})
how do I append data from t1[i] to t2[i]?

Following, a "real" example:

1) first data collection

t1[1] = {A,B,C}
t1[2] = {A1,B1,C1}


t2 = t1
t1 = {}

2)next data collection in t1, t2 already populated with previous data, t2data not yet processed, so "t2 is not empty".

t1[1] = {D,E,F}
t1[2] = {G,H,I}
t1[3] = {J,K,L}


t2[1] = {A,B,C}
t2[2] = {A1,B1,C1}

3) if "t2 is not empty", append data from t1 to t2 so that in the end, t2 values are:


t2[1] = {A,B,C}
t2[2] = {A1,B1,C1}
t2[3] = {D,E,F}
t2[4] = {G,H,I}
t2[5] = {J,K,L}

I suppose I have to use table.insert but after some tests I couldn't figure out how to use it.


Any suggestion is appreciated, thank you in advance :)

kind regards
P.


Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited March 2013 Accepted Answer
    Hello @pie
    I think that there is nothing more sophisticated than looping through second and inserting values in the first one as:
    for i = 1, #t1 do 
        t2[#t2+1] = t1[i]
    end
  • piepie Member
    Thank you very much that works like a charm,
    and now I feel a bit stupid... L-)

Sign In or Register to comment.