Announcement

Collapse
No announcement yet.

Question About copy Tables

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Question About copy Tables

    Code:
    tabl1 = {9,5,8,1,6,0}
    tabl2 = tabl1  -- make a copy of tabl1 befor sorting
    
    Table.Sort(tabl1, nil); -- sort tabl1
    
    result1 = Table.Concat(tabl1, " - ", 1, TABLE_ALL);
    result2 = Table.Concat(tabl2, " - ", 1, TABLE_ALL);
    
    Dialog.Message("Notice", result1.."\r\n"..result2, MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
    Now both Tables are sorted
    0-1-5-6-8-9
    what sort the copy of the origenal table (tabl2) ?????
    what sort tabl2 in my code ?????


  • #2
    i mean
    why tabl2 get sorted ???

    Comment


    • #3
      Read the documentation.

      Ulrich

      Comment


      • #4
        thnaks Ulrich
        i know that

        did tabl1 = tabl2 makes a copy ??
        or make a relative copy ??

        Comment


        • #5
          Here:
          Code:
          tabl1 = {9,5,8,1,6,0};
          tabl2 = {};
          
          for i=1, #tabl1 do
          	tabl2[i] = tabl1[i];
          end
          
          result1 = Table.Concat(tabl1, " - ", 1, TABLE_ALL);
          result2 = Table.Concat(tabl2, " - ", 1, TABLE_ALL);
          
          Dialog.Message("Notice", result1.."\r\n"..result2, MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);

          Comment


          • #6
            thanks Ronin
            same as


            Code:
            for a,z in pairs (tabl1) do
            	tabl2[a] = tabl1[a];
            end
            still confuse about
            tbl2 = tbl1
            why tbl2 clanging when we change tbl1 ??

            ok
            never mind
            thanks all

            Comment


            • #7
              sory
              why tbl2 changes when we change tbl1 ??

              Comment


              • #8
                That's because 'tbl2 = tbl1' doesn't actually create a whole new table, it just links tbl2 to tbl1.

                In a way, it's like domains and hosts. You can create multiple domains (google.com, google.co.uk, google.fr) and link them to the same host.

                Comment


                • #9
                  Link 2 tables ??

                  ok
                  Code:
                  tbl1=tbl2
                  is a negative picture of the compare function :-
                  Code:
                  if tbl1==tbl2 then
                  --do some thing
                  end
                  EXAMPLES

                  not equal example
                  Code:
                  tabl1 = {9,5,8,1,6,0};
                  tabl2 = {4,5,8,1,6,4};
                  
                  if tabl1==tabl2 then
                   Dialog.Message("Notice", "equal", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
                   else
                   Dialog.Message("Notice", "not equal", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1) 
                  end
                  you got "not equal"

                  the equal example
                  Code:
                  tabl1 = {9,5,8,1,6,0};
                  tabl2 = {9,5,8,1,6,0};
                  
                  if tabl1==tabl2 then
                   Dialog.Message("Notice", "equal", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
                   else
                   Dialog.Message("Notice", "not equal", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1) 
                  end
                  you got "equal"

                  Comment


                  • #10
                    Code:
                    tabl1 = {9,5,8,1,6,0};
                    tabl2 = tabl1;
                    The code above simply only copies the reference to the table, not the contents. Sorting or modifying table 1 will also modify table 2.
                    Bas Groothedde
                    Imagine Programming :: Blog

                    AMS8 Plugins
                    IMXLH Compiler

                    Comment


                    • #11
                      thanks Imagine Programming for Explain

                      Comment


                      • #12
                        Originally posted by Imagine Programming View Post
                        Code:
                        tabl1 = {9,5,8,1,6,0};
                        tabl2 = tabl1;
                        The code above simply only copies the reference to the table, not the contents. Sorting or modifying table 1 will also modify table 2.
                        then how can i explain this ??

                        Code:
                        Table1 = {1,2,3,4,5,6,7,8,9,10}
                        Table2 = Table1
                        Table1={}
                        concated1 = Table.Concat(Table1, " , ", 1, TABLE_ALL);
                        concated2 = Table.Concat(Table2, " , ", 1, TABLE_ALL);
                        Dialog.Message("Notice", concated1);
                        Dialog.Message("Notice", concated2);

                        Comment


                        • #13
                          1. You create a table with all numbers from 1 to and including 10 and set the reference to it in the variable Table1.
                          2. You copy the reference to that table to the variable Table2
                          3. You create a new table with nothing in it and set the reference to it in the variable Table1
                          4. You concatenate Table 1 with comma's in between
                          5. You concatenate Table 2 with comma's in between
                          6. You display the concatenated version of Table1 in a message box, which should display an empty string
                          7. You display the concatenated version of Table2 in a message box, which should display the first table.

                          What is happening here, is that you copy the reference of the table in Table1 to Table2. You then overwrite Table1 with a new reference to a new table.

                          This means that the original table still has a reference (in Table2) so it will not have the garbage collector clean it up, it exists via Table2. Table1 however
                          does not have a reference to it anymore, so it now points to an empty table.
                          Bas Groothedde
                          Imagine Programming :: Blog

                          AMS8 Plugins
                          IMXLH Compiler

                          Comment


                          • #14
                            thanks Imagine Programming
                            that was so clear

                            Comment


                            • #15
                              Originally posted by startup View Post
                              thanks Imagine Programming
                              that was so clear
                              You're welcome, good to hear!
                              Bas Groothedde
                              Imagine Programming :: Blog

                              AMS8 Plugins
                              IMXLH Compiler

                              Comment

                              Working...
                              X