Page 1 of 1

Problem/Doubts regarding table values

Posted: Mon Sep 09, 2013 12:14 pm
by turbobay
I am currently working on a function to check my fusion stones.
I want to identify pre-defined patterns as well as check afterwards if i have multiple patterns in my bags.
The identifying process works quite well so far, but i was trying to put the pattern which appear more than once in a table.
i created three tables.
fPattern where i store my "fixed" defined patterns in the file
vPattern where is store alle appearing patterns in vPattern[1] to compare this with the rest in my bag. As i am proceeding all Fusion patterns pass through vPattern[1] as the comparison pattern...i know, i have to check for doubles, but thats for later :-)

tPattern finally is where i store all the patterns which i found appear multiple times (at least 2 times)

My problem is, when printing tPattern ALL patterns are the same and are the LAST vPattern i found and moved into the table. So i may certainly mess up with the table.
Perhaps i make something completely stupid, but i don't know other syntax for the move^^

Code: Select all

fPattern = {}
fPattern[1] = {	Count = 0,
				Stat1 = "Ausdauer I",
				Stat2 = "Intellekt I"}
fPattern[2] = {	Count = 0,
				Stat1 = "Ausdauer I",
				Stat2 = "Intellekt I",
				Stat3 = "Fähigkeit"}	
vPattern = {}
vPattern[1] = {	Count = 0,
				Stat1 = "Dummy",
				Stat2 = "Dummy",
				Stat3 = "Dummy"}
tPattern = {}

Code: Select all

-- item is actual Fusion stone in bag to be verified
-- vPattern is actual pattern to search for
-- check_Fusion returns true if the item / Fusion Stone Stats correspond to the vPattern
if check_Fusion(item,vPattern) then
	tPatternC = tPatternC +1
	tPattern[tPatternC] = vPattern[1]
	printf(item.Name.." in Slot: "..i.."\n")													
end	
printPattern(tPattern)
My problem is the line:
tPattern[tPatternC] = vPattern[1]

After that all entries of tPattern are the same :-( and are same as LAST vPattern[1]
tPattern[1]
tPattern[2]
tPattern[3]
tPattern[4]
tPattern[5]
tPattern[6]
...

Where is my fault ?

edit:
I tried to assign different values directly...in the end all tPattern.Count have the value 22....

Code: Select all

vPattern[1].Count = 7
tPattern[1] = vPattern[1]
vPattern[1].Count = 9
tPattern[2] = vPattern[1]
vPattern[1].Count = 22
tPattern[14] = vPattern[1]
edit 2:
I also tried

Code: Select all

table.insert(tPattern,vPattern[1])
But the same result

edit 3:
Well i am not really good in lua...but is it possible, that with moving vPattern[1] into the table will be stored the adress/pointer to that vPattern[1] only and NOT the actual values ?
That would explain, why all entries change value once changing vPattern. But if so, how to move it by value and not by address ?

Re: Problem/Doubts regarding table values

Posted: Mon Sep 09, 2013 2:36 pm
by rock5
When copying tables, eg. tableB = tableA, it doesn't actually copy it. It sets tableB to point to the same table in memory that tableA points to. So if you change a value of one table you change the other also.

In this case you can use the table.copy command, eg.

Code: Select all

tPattern[tPatternC] = table.copy(vPattern[1])
In other cases you can also create a temporary local table variable to hold the table you will copy. Being a local variable means that it wont get changed when it goes out of scope.