jbrave
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2011
06:46 PM
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?
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!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
7 REPLIES 7
kbenson
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2011
07:12 PM
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!
Check out Reversi! in the channel store!
jbrave
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2011
07:29 PM
Re: array super weirdness...
Yeah that is what appears to be happening. Not only that, but even if:
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
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!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!
renojim
Community Streaming Expert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2011
07:55 PM
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:
I'd be interested to know if there's an easier/better way.
-JT
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.
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.
jbrave
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2011
07:58 PM
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
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!
Musiclouds: The best free internet music, on your Roku!
Ouroborialis: Psychedelic Screensaver for Roku!

TheEndless
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2011
08:01 PM
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)
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
kbenson
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2011
08:07 PM
Re: array super weirdness...
Man, I've been really lax about getting stuff committed to librokudev lately...
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"
[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!
Check out Reversi! in the channel store!
renojim
Community Streaming Expert
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2011
08:09 PM
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.
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.