Log In  


Was just experimenting with arrays, starting to understand NEW and DEL a little better, when I came across this:

set={}
for i=1,6 do
  set[i]=i
end
newset=set
newset[3]=0
?set[3]

What gives ? The correct answers should be "3."

Normal BASIC with "newset=set" would just copy the array to a new array and let you work with it as a new array. You can't do this in Pico-8 as apparently it's just a shortcut to the original array ?

Is there a way to copy one array to another easily ? Like newset=set{} or something ?



>What gives ? The correct answers should be "3."

Or should it? Just as Lua passes tables into functions by reference, it also assigns them by reference. If you want to copy a table you will have to do it yourself. Though you will have to consider, if the table being copied contains a table, is the inner table copied by reference or by value?


Thanks, @Shoxidizer.

All I know is back in good old QBasic or even BlitzMAX for that matter, you can tell an array to equal another array and you have a 2nd entirely different array.

global array1:int={3,4}
global array2=array1
array2[1]=0
print array1[1]

Result will be 3.

We got lotsa memory to do this. But apparently ... that is not the case in Pico-8. I was hoping there was some quick short-cut to truly copy one array to another outside of the tedious FOR/NEXT method.


In Lua, tables (arrays) and functions are objects. When you create a variable with a table or function, the variable does not contain the object itself, just a reference to it. So your set variable doesn't actually contain a table (array). It contains a reference to a table. When you do newset=set, you're just telling newset to also store the same reference as the set variable. So you get exactly what you would expect in that situation, which is set[3] referring to the same exact table value as newset[3].

Here's the exact information on this from the Lua language: http://www.lua.org/manual/5.1/manual.html#2.2


Looking over the link you posted. Heating up a fresh cup of coffee to sit down and read it ... Actually reading from the top. :)

@MBoffin, early on is there a command specifically in Pico-8 to copy an array to a unique and new array ? I know in blitz there is ARRAYFILL which neatly fills an array.

Some type of ARRAYCOPY() perhaps ?


I can not find any Pico-8 feature for copying tables, but these should be able to do what you need

function shallow_copy(table)
	local new_table={}
	for key, value in pairs(table) do
		new_table[key]=value
	end
	return new_table
end
function deep_copy(table)
	if type(table)=="table" then
		local new_table={}
		for key, value in pairs(table) do
			new_table[key]=deep_copy(value)
		end
		return new_table
	else
		return table
	end
end

The first one performs a shallow copy. That means that any tables in the table it is given will be the exact same tables it puts in the copy.

The second is a deep copy. That means that any tables in the table it is given will copied to put in the new table. It is recursive so even if you have a table in a table in a table each one will be copied into a new separate table.


The deep copy is, if you'll pardon the expression, deep. Quite interesting to look at. I think - I think I was trying something like this a year ago or so, to copy one array to another.

I got as far as knowing the type needed to be TABLE but then got bogged down as to what to do next.

Very nice copy array function, @Shoxidizer !



[Please log in to post a comment]