Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
jbrave
Channel Surfer

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?
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
7 REPLIES 7
kbenson
Visitor

Re: array super weirdness...

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.
-- GandK Labs
Check out Reversi! in the channel store!
0 Kudos
jbrave
Channel Surfer

Re: array super weirdness...

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
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
renojim
Community Streaming Expert

Re: array super weirdness...

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
Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
0 Kudos
jbrave
Channel Surfer

Re: array super weirdness...

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
Screenshades: The first Screensaver for Roku2!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
0 Kudos
TheEndless
Channel Surfer

Re: array super weirdness...

"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.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
kbenson
Visitor

Re: array super weirdness...

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"
-- GandK Labs
Check out Reversi! in the channel store!
0 Kudos
renojim
Community Streaming Expert

Re: array super weirdness...

"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
Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.