Log In  

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

for i=1,6 do

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 ?

P#69666 2019-11-05 20:27 ( Edited 2019-11-05 20:29)

>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?

P#69667 2019-11-05 20:54
:: dw817

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
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.

P#69668 2019-11-05 21:13 ( Edited 2019-11-05 21:20)

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

P#69669 2019-11-05 21:16
:: dw817

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 ?

P#69670 2019-11-05 21:22 ( Edited 2019-11-05 21:44)

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
    return new_table
function deep_copy(table)
    if type(table)=="table" then
        local new_table={}
        for key, value in pairs(table) do
        return new_table
        return table

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.

P#69690 2019-11-06 18:46
:: dw817

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 !

P#69694 2019-11-07 00:44

[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2020-06-04 09:25 | 0.040s | 2097k | Q:24