CAN SOMEONE EXPLAIN THE FOLLOWING CODE PLEASE, I AM STUCK HERE

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
DebasishDatta
Prole
Posts: 17
Joined: Fri Nov 26, 2021 5:15 am

CAN SOMEONE EXPLAIN THE FOLLOWING CODE PLEASE, I AM STUCK HERE

Post by DebasishDatta »

Code: Select all

push = require 'push'

WINDOW_WIDTH  = 1280
WINDOW_HEIGHT = 720
VIRTUAL_WIDTH = 512
VIRTUAL_HEIGHT = 288
temp1,temp2,temp3={},{},{}
function love.load()
	testTable ={{{x=1,y=1},{x=1,y=2},{x=1,y=3},{x=1,y=4}}}
	testString = 'BEFORE SWAP'
	love.graphics.setDefaultFilter('nearest','nearest')	
	push:setupScreen(VIRTUAL_WIDTH,VIRTUAL_HEIGHT,WINDOW_WIDTH,WINDOW_HEIGHT,{
			fullscreen = false,
			vsync = true,
			resizeble= true,
			})
end

function love.keypressed(key)
	if key == 'escape' then
		love.event.quit()
	end
	if key == 'enter' or key == 'return' then
		temp1,temp2 = testTable[1][1],testTable[1][2]		
		--local tempx, tempy = temp1.x,temp1.y
		--temp1.x,temp1.y = temp2.x,temp2.y
		--temp2.x,temp2.y = tempx,tempy		
		temp3 = temp1
		--temp1 = temp2
		--temp2 = temp3
		--temp1.x = 9
		testTable[temp1.x][temp1.y] = temp2
		testTable[temp2.x][temp2.y] = temp3
		temp1.x = 9
		testString = 'AFTER SWAP'
	end
end

function love.update(dt)

end

function love.draw()
	push:start()
		local count = 1
		love.graphics.print(testString, VIRTUAL_WIDTH/2,VIRTUAL_HEIGHT/2 - 20)
		for k, element1 in pairs(testTable) do			
			for m,element in pairs(element1) do
				count = count + 1
				love.graphics.print(tostring(element.x)..tostring(element.y),VIRTUAL_WIDTH/2 + (count)*10, VIRTUAL_HEIGHT/2)
				count = count + 1
			end
			break
		end
		love.graphics.print(tostring(temp1.x)..' '..tostring(temp1.y),0,VIRTUAL_HEIGHT - 50)
		love.graphics.print(tostring(temp2.x)..' '..tostring(temp2.y),0,VIRTUAL_HEIGHT - 40)
		love.graphics.print(tostring(temp3.x)..' '..tostring(temp3.y),0,VIRTUAL_HEIGHT - 30)
	push:finish()
end
In the above code I am trying to interchange two table which are inside another table. In the line : temp1.x = 9 , I have assigned a value. The output is as follows:-
AFTER SWAP
12 91 13 14

91
12
91

My doubt is that , whether temp1,temp2 and temp3 have the values or references and how the interchange is working here. Please someone put some light on it.
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: CAN SOMEONE EXPLAIN THE FOLLOWING CODE PLEASE, I AM STUCK HERE

Post by MrFariator »

Your 'temp' variables contain references to the tables, and as such modifying temp1 will also affect temp3, because they contain a reference to the same table. If you want to copy a table you'll have to recreate it. I added some comments to your code below to illustrate what's happening.

Code: Select all

testTable ={
  {
    {x=1,y=1}, -- table A 
    {x=1,y=2}, -- table B
    {x=1,y=3}, -- table C
    {x=1,y=4}, -- table D
  }
}
-- ...
-- assign references to tables A and B to variables temp1 and temp2
temp1,temp2 = testTable[1][1],testTable[1][2]   

-- assign reference to table A to temp3
-- now both temp1 and temp3 point to same table
temp3 = temp1

-- in the test table, reassign table B to A's place, because temp1 references A, which contains (x=1,y=1) at this point 
-- at this point the testTable has B in two places, A is not present at all
testTable[temp1.x][temp1.y] = temp2

-- in the test table, reassign table A to B's place, because temp2 references B, which contains (x=1,y=2) at this point 
testTable[temp2.x][temp2.y] = temp3

-- temp1 and temp3 hold a reference to table A
-- as such...
temp1.x = 9 -- is the same as
temp3.x = 9 -- this line

-- now the table structure is as follows:
--[[
testTable ={
  {
    {x=1,y=2}, -- table B 
    {x=9,y=1}, -- table A
    {x=1,y=3}, -- table C
    {x=1,y=4}, -- table D
  }
}
]]
DebasishDatta
Prole
Posts: 17
Joined: Fri Nov 26, 2021 5:15 am

Re: CAN SOMEONE EXPLAIN THE FOLLOWING CODE PLEASE, I AM STUCK HERE

Post by DebasishDatta »

MrFariator wrote: Wed Dec 15, 2021 8:03 am Your 'temp' variables contain references to the tables, and as such modifying temp1 will also affect temp3, because they contain a reference to the same table. If you want to copy a table you'll have to recreate it. I added some comments to your code below to illustrate what's happening.

Code: Select all

testTable ={
  {
    {x=1,y=1}, -- table A 
    {x=1,y=2}, -- table B
    {x=1,y=3}, -- table C
    {x=1,y=4}, -- table D
  }
}
-- ...
-- assign references to tables A and B to variables temp1 and temp2
temp1,temp2 = testTable[1][1],testTable[1][2]   

-- assign reference to table A to temp3
-- now both temp1 and temp3 point to same table
temp3 = temp1

-- in the test table, reassign table B to A's place, because temp1 references A, which contains (x=1,y=1) at this point 
-- at this point the testTable has B in two places, A is not present at all
testTable[temp1.x][temp1.y] = temp2

-- in the test table, reassign table A to B's place, because temp2 references B, which contains (x=1,y=2) at this point 
testTable[temp2.x][temp2.y] = temp3

-- temp1 and temp3 hold a reference to table A
-- as such...
temp1.x = 9 -- is the same as
temp3.x = 9 -- this line

-- now the table structure is as follows:
--[[
testTable ={
  {
    {x=1,y=2}, -- table B 
    {x=9,y=1}, -- table A
    {x=1,y=3}, -- table C
    {x=1,y=4}, -- table D
  }
}
]]
Thank you very much, for your reply. It was very helpful. It seems that table A,B,C,D are separate entity , hence when they changes their places, still temp variables points to them. I specially want to mention the line

Code: Select all

testTable[temp1.x][temp1.y] = temp2
it is equivalent to

Code: Select all

temp1 = temp2 
, this is most confusing. Again thank you for your valuable time.
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: CAN SOMEONE EXPLAIN THE FOLLOWING CODE PLEASE, I AM STUCK HERE

Post by MrFariator »

You're welcome, but I have to point out that these two lines are not equivalent:

Code: Select all

testTable[temp1.x][temp1.y] = temp2
temp1 = temp2 
The first one reassigns whatever temp2 is holding into testTable[1][1] (basically replacing "A" with whatever temp2 holds). If that position contained a table, it is effectively removed and will be garbage collected eventually if no other place holds a reference to it. Your temp variables are globals, so they will keep whatever they hold until they're given something else. A local variable would lose the reference once the scope exits, like if it was a local declared inside a function.

The second line reassigns whatever temp2 holds into temp1. This will not modify contents of testTable in any shape or form.

If these two lines are done back to back, and temp3 doesn't hold a reference to A, then A is effectively out of scope and will be garbage collected eventually.

To understand this, I think it's good to look up what's a "reference" in programming terms. The lua reference manual, in section 2.2, quickly mentions that tables are handled by reference, where basically everything else (numbers, booleans, strings) are handled by value.
DebasishDatta
Prole
Posts: 17
Joined: Fri Nov 26, 2021 5:15 am

Re: CAN SOMEONE EXPLAIN THE FOLLOWING CODE PLEASE, I AM STUCK HERE

Post by DebasishDatta »

MrFariator wrote: Wed Dec 15, 2021 6:10 pm You're welcome, but I have to point out that these two lines are not equivalent:

Code: Select all

testTable[temp1.x][temp1.y] = temp2
temp1 = temp2 
The first one reassigns whatever temp2 is holding into testTable[1][1] (basically replacing "A" with whatever temp2 holds). If that position contained a table, it is effectively removed and will be garbage collected eventually if no other place holds a reference to it. Your temp variables are globals, so they will keep whatever they hold until they're given something else. A local variable would lose the reference once the scope exits, like if it was a local declared inside a function.

The second line reassigns whatever temp2 holds into temp1. This will not modify contents of testTable in any shape or form.

If these two lines are done back to back, and temp3 doesn't hold a reference to A, then A is effectively out of scope and will be garbage collected eventually.

To understand this, I think it's good to look up what's a "reference" in programming terms. The lua reference manual, in section 2.2, quickly mentions that tables are handled by reference, where basically everything else (numbers, booleans, strings) are handled by value.
Thank you once again for your insightful remark and the material, I really appreciate your effort and time, it is really a huge help for me. Please put some more light on the following line :

Code: Select all

testTable[temp1.x][temp1.y] = temp2
immediately after the above line if I assign

Code: Select all

 temp1.x = 7 
will it affect table B or table A, if B then how temp3 is related to temp1, if possible can you please draw a picture mentioning the memory location related to the above interchange operation, that will clear most of my doubt?
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: CAN SOMEONE EXPLAIN THE FOLLOWING CODE PLEASE, I AM STUCK HERE

Post by MrFariator »

Assuming that at the time of the assignment, temp1 points to A, and temp2 points to B, then in the testTable A's position is overwritten by B. This does not mark A for garbage collection, because temp1 still holds onto A. After that when you modify temp1 (assuming it still holds A, and hasn't been reassigned), A will be modified. If temp3 also happens to hold a reference to A when this takes place, then any changes to A will also be reflected on it - because temp1 and temp3 in this scenario point to the same table.

Here is some quick code to give some further illustration:

Code: Select all

-- original table
local t = { 1, 2, 3 }

-- assign the table to another variable
local r = t
print ( r == t ) -- true, both 'r' and 't' refer to same table

r[2] = 5
t[1] = 3

-- print the values; we can see that since both 't' and 'r' point to same table,
-- the output will be the same after the modifications
print(t[1],t[2],t[3]) -- prints: 3, 5, 3
print(r[1],r[2],r[3]) -- prints: 3, 5, 3

-- assign the table to yet another variable
local r2 = r
print ( r2 == r ) -- true, tables are same
print ( r2 == t ) -- true, tables are same

-- modifying r2 will affect all three, since they point to the same table
r2[3] = 0
print ( r2[3] ) -- prints: 0
print ( r[3]  ) -- prints: 0
print ( t[3]  ) -- prints: 0

-- reassign a different table to 'r', copying the values from 't'
-- this basically creates a copy of the table, but it's not the same table in memory anymore
r = { t[1], t[2], t[3] }
print ( r  == t ) -- false, the tables are NOT the same anymore, even though they contain the same values!
print ( r2 == t ) -- true, r2 and t still refer to the same table

-- set the original variable to nil
t = nil
print ( t  ) -- prints: nil
print ( r  ) -- table: 0x020810d8 
print ( r2 ) -- table: 0x02081088 <- this is still the original table created in the first line
Note at the end there how the output prints "table: 0x020810d8" or a similar number. Those numbers are basically random from run to run, so you might see different output on your end. However, if two variables hold a reference to the same table while the code is running, the numbers will always match.

The above code basically extends to all instances where you might be handling code. Variables holds references to tables, so modification in one place will affect the others. In your case you're modifying and swapping the tables 'A' and 'B' around, so any place that points to either of them will be affected. However, in your code only testTable holds a reference to the table containing the tables A-D, so arranging the tables' orders around will only affect testTable.

A very crude graph can be made about all of this:
Image
(Note: this is just for simple illustration, don't take this as gospel for how things work under the hood)

Here, variables var1, var2, var3 all point to 'A'. If you modify the table contained within the variables, 'A' will be affected. This extends to 'B' and 'D' with the respective variables. However, 'C', is only assigned to a single variable, so modifying var6 will only affect 'C'.

You could extend this graph out further when dealing with tables containing tables (like your testTable), and the principle will remain the same.
DebasishDatta
Prole
Posts: 17
Joined: Fri Nov 26, 2021 5:15 am

Re: CAN SOMEONE EXPLAIN THE FOLLOWING CODE PLEASE, I AM STUCK HERE

Post by DebasishDatta »

Thank you sir for your response, it was very helpful. It may sounds silly but I still have doubt. As I have gone through your example following code from my post creates doubt

Code: Select all

temp1,temp2 = testTable[1][1],testTable[1][2]					
		temp3 = temp1		
		testTable[temp1.x][temp1.y] = temp2
		testTable[temp2.x][temp2.y] = temp3
both temp1,temp3 points to the same table , that is A as you said. Now when the line

Code: Select all

 testTable[temp1.x][temp1.y] = temp2 
executes temp1 should be modified and therefore temp3 also as they reference to the same table. If not then in place of

Code: Select all

 testTable[temp2.x][temp2.y] = temp3 

Code: Select all

testTable[temp2.x][temp2.y] = temp1 
should work. Can you please explain the above?
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: CAN SOMEONE EXPLAIN THE FOLLOWING CODE PLEASE, I AM STUCK HERE

Post by MrFariator »

Consider the following as its own code, separate from the code you've been posting.

Code: Select all

testTable ={
  {
    {x=1,y=1}, -- A
    {x=1,y=2}, -- B
    {x=1,y=3}, -- C
    {x=1,y=4}, -- D
  }
}

-- assign A and B to temp1 and temp2
temp1,temp2 = testTable[1][1],testTable[1][2]         

-- assign temp1's value to temp3, they now both reference A
temp3 = temp1   

-- temp1.x == 1
-- temp1.y == 1
-- as such, we're assigning temp2 to position [1][1] in the testTable
-- this overwrites the first slot of testTable[1], which used to be A, with B
testTable[temp1.x][temp1.y] = temp2

-- structure is now as follows:
--[[
testTable ={
  {
    {x=1,y=2}, -- B
    {x=1,y=2}, -- B
    {x=1,y=3}, -- C
    {x=1,y=4}, -- D
  }
}

temp1 is the same as A
temp2 is the same as B
temp3 is the same as A
]]

-- temp2.x == 1
-- temp2.y == 2
-- as such, we're assigning temp3 to position [1][2] in the testTable
-- this overwrites the second slot of testTable[1], which used to be B, with A
testTable[temp2.x][temp2.y] = temp3

-- structure is now as follows:
--[[
testTable ={
  {
    {x=1,y=2}, -- B
    {x=1,y=2}, -- A
    {x=1,y=3}, -- C
    {x=1,y=4}, -- D
  }
}

temp1 is the same as A
temp2 is the same as B
temp3 is the same as A
]]

-- temp2.x == 1
-- temp2.y == 2
-- as such, we're assigning temp3 to position [1][2] in the testTable
-- because that position is already occupied by A, basically nothing happens
testTable[temp2.x][temp2.y] = temp3 

-- same as above
testTable[temp2.x][temp2.y] = temp1 
The above is only swapping the tables' orders around inside testTable[1]. You could replace all the "temp#.x" and "temp#.y" with number values and it'd be the same. This code is not modifying anything but the contents of testTable[1], which itself is a table containing A, B, C and D. A and B are not being directly touched here, other than the lookups to the x and y values they contain.

If your goal is to just swap the contents of testTable[1], the code above succeeds. However, if you wanted to, say, swap the values temp1 and temp2 hold, you'd write something along the lines of

Code: Select all

local var1 = 1
local var2 = 2

local t = var2
var2    = var1
var1    = t
print(var1,var2) -- prints: 2, 1

-- OR
local var1 = 1
local var2 = 2
var1,var2  = var2,var1

print(var1,var2) -- prints: 2, 1
DebasishDatta
Prole
Posts: 17
Joined: Fri Nov 26, 2021 5:15 am

Re: CAN SOMEONE EXPLAIN THE FOLLOWING CODE PLEASE, I AM STUCK HERE

Post by DebasishDatta »

Bingo!!! your example is the bullseye this time. Thank you very much sir, This is what I wanted. Kindly see the part of example you have illustrated:-

Code: Select all

-- structure is now as follows:
--[[
--testTable ={
--{
--    {x=1,y=2}, -- B
--    {x=1,y=2}, -- B
--    {x=1,y=3}, -- C
--    {x=1,y=4}, -- D
--  }
--}

--temp1 is the same as A
--temp2 is the same as B
--temp3 is the same as A
whether the following will work? If no why? why we have to assign the value of temp3 in testTable[temp2.x][temp2.y], if both temp1 and temp3 have the same value.

Code: Select all

testTable[temp2.x][temp2.y] = temp1 

-- same as above
testTable[temp2.x][temp2.y] = temp3
DebasishDatta
Prole
Posts: 17
Joined: Fri Nov 26, 2021 5:15 am

Re: CAN SOMEONE EXPLAIN THE FOLLOWING CODE PLEASE, I AM STUCK HERE

Post by DebasishDatta »

At last my doubt is clear any of the following lines

Code: Select all

testTable[temp2.x][temp2.y] = temp1 
testTable[temp2.x][temp2.y] = temp3
will work. Thank you for your effort and time. The examples you used to elaborate the topic were excellent and very easy to understand. It will be nice to have conversation like this with you in future. Thank you once again.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 63 guests