What am I doing wrong in this table example? (SOLVED)

Here is the code:

array1 = {}  
array2 = {  
 value1 = "",  
 value2 = "",  
 value3 = ""  
}  
  
for a=1,10 do  
 array1[a] = array2  
 array1[a].value1 = a  
end  
  
for a=1,10 do  
 print(array1[a].value1)  
end  

The goal is to create one table inside another table (array2 inside array1) then set a value in the second table (value1 of array2 in this example) to individual values (a in this example).

When I run the program there are no errors but it says the value for each of array1[1-10].value1 are all equal to 10 when array1[1].value1 should be equal to 1, array1[2].value1 should be equal to 2, etc…

Thanks in advance for the help.

Jason [import]uid: 142486 topic_id: 26847 reply_id: 326847[/import]

@jason 714,

Try this and see if it’s want you wanted…

You have the Idea, just printing…error…:slight_smile:

array1 = {};  
  
array2 = {value1 = "", value2 = "", value3 = ""};  
   
   
   
for a=1,10 do  
 array1[a] = array2;  
 array1[a].value1 = a;  
 array1[a].value2 = a;  
 array1[a].value3 = a;  
 print("array1[a].value1",array1[a].value1);  
 print("array1[a].value2",array1[a].value2);  
 print("array1[a].value3",array1[a].value3);  
  
 print("End of LOAD array1 loop....");  
  
end  
   
--for a=1,10 do  
 -- print(array1[a].value1)  
--end  
  

Hope that helps ya.
Larry
[import]uid: 107633 topic_id: 26847 reply_id: 108968[/import]

Unfortunately this does not work either. I modified the bottom of your example so it now reads:

array1 = {};  
   
array2 = {value1 = "", value2 = "", value3 = ""};  
   
   
   
for a=1,10 do  
 array1[a] = array2;  
 array1[a].value1 = a;  
 array1[a].value2 = a;  
 array1[a].value3 = a;  
 print("array1[a].value1",array1[a].value1);  
 print("array1[a].value2",array1[a].value2);  
 print("array1[a].value3",array1[a].value3);  
  
 print("End of LOAD array1 loop....");  
  
end  
   
print("second Printing")  
   
for a=1,10 do  
 print(array1[a].value1)  
end  

When it does the second printing (listing at the very bottom) it still says that array1[a].value1 is equal to 10 each time when it should be equal to 1 on the first iteration, 2 on the second, etc…

It acts as if it is changing the same variable with each iteration when it should be changing a different variable with each iteration.

Jason [import]uid: 142486 topic_id: 26847 reply_id: 108971[/import]

Strange! Can’t see why your first example shouldn’t work. The weirdest thing about it is that if you place the print statement in your first loop it actually prints the correct values.

Anyway, this might help you:

[code]
array1 = {}

for a=1,10 do
array1[a] = {value1,value2,value3}
array1[a].value1 = a
end

for a=1,10 do
print(array1[a].value1)
end
[/code] [import]uid: 129287 topic_id: 26847 reply_id: 108976[/import]

It appears you have a pointer vs. copy problem. Declaring the following function before your code is needed:

-- Creates a complete/deep copy of the data  
function deepCopy(object)  
 local lookup\_table = {}  
 local function \_copy(object)  
 if type(object) ~= "table" then  
 return object  
 elseif lookup\_table[object] then  
 return lookup\_table[object]  
 end  
 local new\_table = {}  
 lookup\_table[object] = new\_table  
 for index, value in pairs(object) do  
 new\_table[\_copy(index)] = \_copy(value)  
 end  
 return setmetatable(new\_table, getmetatable(object))  
 end  
 return \_copy(object)  
end  

Then, in your original code, you only need to change one line (just a little):

array1 = {}  
array2 = {  
 value1 = "",  
 value2 = "",  
 value3 = ""  
}  
   
for a=1,10 do  
 array1[a] = deepCopy(array2) -- Only this needs to be changed  
 array1[a].value1 = a  
end  
   
for a=1,10 do  
 print(array1[a].value1)  
end   

Pointers vs. actual values… Tried it, and it prints 1 - 10 for me. [import]uid: 79933 topic_id: 26847 reply_id: 109000[/import]

mpappas is correct.

Here is an alternative solution, which generates a new ‘array2’ inside every loop

[code]
array1 = {};

for a=1,10 do
local array2 = {value1 = “”, value2 = “”, value3 = “”};
array1[a] = array2;
array1[a].value1 = a;
array1[a].value2 = a;
array1[a].value3 = a;
print(“array1[a].value1”,array1[a].value1);
print(“array1[a].value2”,array1[a].value2);
print(“array1[a].value3”,array1[a].value3);

print(“End of LOAD array1 loop…”);

end

print(“second Printing”)

for a=1,10 do
print(array1[a].value1)
end
[/code] [import]uid: 108660 topic_id: 26847 reply_id: 109004[/import]

This solved the problem perfectly. Thanks again to everyone for the help.

Jason [import]uid: 142486 topic_id: 26847 reply_id: 109082[/import]