This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CCJJSax's profile picture

Can't figure out what is deleting my table

Started by CCJJSax, 20 August 2019 - 03:07 AM
CCJJSax #1
Posted 20 August 2019 - 05:07 AM
I have a shortened version of my code, but it does the same weirdness. I have table (t) that only has one item in it, which is an array. After I run it through highScore() it changes to 0 even though I only reference it as tab, then again through pTab. The goal of highScore() is to return an list of all items in t in order by score. Why is this happening?


local function highScore(tab, spot)
local pTab = tab
local scores = {}
local winner
while #pTab > 0 do
  winner = {key = 0, score = 0}
  for k,v in pairs(pTab) do
   -- print(v.score)
   if v.score > winner.score then
    winner = {key = k, score = v.score}
   end
  end
  scores[#scores+1] = pTab[winner.key]
  table.remove(pTab, winner.key)
end
return scores
end
t = {
  {
    player = "TEST",
    score = 0,
  },
}
print(#t) -- prints 1 as it should
h = highScore(t)
print(#t) -- prints 0 when it should still be 1
Luca_S #2
Posted 20 August 2019 - 06:27 AM
See here.

A table in Lua is only a reference, like objects in Java. This means that any changes you do to the parameter in the highScore function as a side effect changes the original value.
If you really want to copy a table instead of doing
pTab = tab
you need to use something like this function:

function copyTable(src, dest)
  for k, v in pairs(src) do
	if type(v) ~= "table" then
	  dest[k] = v
	else
	  dest[k] = {}
	  copyTable(v, dest[k])
	end
  end
end

Also you could just sort the scores table like this:

table.sort(scores, function(a, B)/>/> return a.score > b.score end)

If you use that in the highScore function that will of course as a side effect sort the "t" table. But if the original order of the scores doesn't matter you can just do it like this. Else you need to first copy the table using the function above and then sort the new table.
Edited on 01 September 2019 - 04:33 AM
CCJJSax #3
Posted 20 August 2019 - 07:06 AM
See here.

A table in Lua is only a reference, like objects in Java. This means that any changes you do to the parameter in the highScore function as a side effect changes the original value.
If you really want to copy a table instead of doing
pTab = tab
you need to use something like this function:

function copyTable(src, dest)
dest = {}
for k, v in pairs(src) do
  if type(v) ~= "table" then
	dest[k] = v
  else
	dest[k] = {}
	copyTable(v, dest[k])
  end
end

Also you could just sort the scores table like this:

table.sort(scores, function(a, B)/>/> return a.score > b.score end)

If you use that in the highScore function that will of course as a side effect sort the "t" table. But if the original order of the scores doesn't matter you can just do it like this. Else you need to first copy the table using the function above and then sort the new table.

That's actually what I ended up doing. just did a pairs iteration and cloned the table that way, then learned about table.sort. I swear I've done stuff like highScore numerous times in the past without issue. Thanks for helping out!