Forum Discussion

jbrave's avatar
jbrave
Channel Surfer
15 years ago

array super weirdness...

BrightScript Debugger> x=["one","two","three"]
BrightScript Debugger> y=x
BrightScript Debugger> ?x
one
two
three

BrightScript Debugger> ?y
one
two
three

BrightScript Debugger> y[0]="four"
BrightScript Debugger> ?y
four
two
three

BrightScript Debugger> ?x
four
two
three

What the heck just happened here? Why does a change to y affect x? How do I create an independant copy of an array that doesn't change when the copy changes?
  • x actually contains a reference (vm memory address) to the array. Copying x to y copies the reference, so they both point to the same array.
  • jbrave's avatar
    jbrave
    Channel Surfer
    Yeah that is what appears to be happening. Not only that, but even if:

    sub main()
    x=["one","two","three"]


    something(x)

    ?"x=";x
    stop
    end sub

    function something(y)
    y[0]="four"
    ?"y=";y
    sleep(10000)
    end function


    BrightScript Debugger> ^[[A------ Running ------
    y=four
    two
    three

    x=four
    two
    three


    so a change in a sub or function changes the variable even outside the scope of the sub or function even when there is nothing returned from the function.

    So how can I copy the contents of an array to another array without creating a refernce?

    - Joel
  • renojim's avatar
    renojim
    Community Streaming Expert
    I don't know if there's another way, but when I need to do something like this I create a new object and then iterate over all the members of the object I want to copy. For example:

    x = []
    i = 0
    for each el in y
    x[i] = el
    i = i+1
    end for

    I'd be interested to know if there's an easier/better way.

    -JT
  • jbrave's avatar
    jbrave
    Channel Surfer
    Seems so weird - I mean, if x=6 and y=x, you can add y=y+1 and x does not change, what would be the purpose of not sticking with this normal behaviour in an array?

    I wish there was some kind of assignment operator that would force a copy of an array that does not just create a pointer. Maybe there is an undocumented one?

    - Joel
  • "renojim" wrote:
    I don't know if there's another way, but when I need to do something like this I create a new object and then iterate over all the members of the object I want to copy. For example:

    x = []
    i = 0
    for each el in y
    x[i] = el
    i = i+1
    end for

    I'd be interested to know if there's an easier/better way.

    -JT

    All objects are passed by reference, so even with this, the values in the array are references, unless they are base types (string, integer, boolean, etc), so to get a true copy, you'd need to recreate the object and copy all the way down the chain to the base types. If you want an "easy" way to do it, passing them through the SimpleJSONBuilder/SimpleJSONParser would result in a copy, as it serializes and deserializes the whole object.
  • Man, I've been really lax about getting stuff committed to librokudev lately...


    [kbenson@brinstar source]$ cat rdDeepCopy.brs
    function rdDeepCopy(v as object) as object
    v = box(v)
    vType = type(v)
    if vType = "roArray"
    n = CreateObject(vType, v.count(), true)
    for each sv in v
    n.push(rdDeepCopy(sv))
    end for
    elseif vType = "roList"
    n = CreateObject(vType)
    for each sv in v
    n.push(rdDeepCopy(sv))
    end for
    elseif vType = "roAssociativeArray"
    n = CreateObject(vType)
    for each k in v
    n[k] = rdDeepCopy(v[k])
    end for
    elseif vType = "roByteArray"
    n = CreateObject(vType)
    n.fromHexString( v.toHexString() )
    elseif vType = "roXMLElement"
    n = CreateObject(vType)
    n.parse( v.genXML(true) )
    elseif vType = "roInt" or vType = "roFloat" or vType = "roString" or vType = "roBoolean" or vType = "roFunction" or vType = "roInvalid"
    n = v
    else
    print "skipping deep copy of component type "+vType
    n = invalid
    'n = v
    end if
    return n
    end function



    Probably not complete, but it will warn you of any types it doesn't explicitly support. You can change the behavior for unsupported types by uncommenting the line below "n = invalid"
  • renojim's avatar
    renojim
    Community Streaming Expert
    "TheEndless" wrote:
    All objects are passed by reference, so even with this, the values in the array are references, unless they are base types (string, integer, boolean, etc), so to get a true copy, you'd need to recreate the object and copy all the way down the chain to the base types.

    True. I hadn't really considered that. I think I've only created copies of objects that only contain base types.

    -JT