EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2012
07:21 PM
How to clone roAssociativeArray?
Say i have dictionary (assoc.array) aa and want to copy it to bb but so that my further mucking with bb does not affect the original aa (thus just assigning the reference as bb=aa won't do). I looked fore clone() or copy() method but none such. So how is it done? All i can think of is unrefined, exploiting that roAssociativeArray implements ifEnum:
bb = {}
for each key in aa:
bb[key] = aa[key] 'shallow copy
end for
4 REPLIES 4

RokuJoel
Binge Watcher
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2012
10:41 AM
Re: How to clone roAssociativeArray?
EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2012
01:49 PM
Re: How to clone roAssociativeArray?
"RokuJoel" wrote:
http://forums.roku.com/viewtopic.php?f=34&t=36995&p=240814&hilit=copy+array#p240811
Thanks, that's a valiant effort at deepcopy on kbenson's side. Serialize/deserialize idea is cute, too. For the record, both will get in trouble with cyclic structures though (infinite loop). Deep-copy is a can of worms so nevermind that, let's talk shallow copy.
Is there no better way known to Roku of doing roAssociativeArray copy than the "manu-matic" way i show above?
(no intent to criticize here, just would like yes/no determination)
PS. why may one need that you ask? well since objects are emulated with roAssocArrays but there are no classes/constructors, one has to create objects by cloning prototype objects... a-la javascript
EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2014
11:18 AM
Re: How to clone roAssociativeArray?
"EnTerr" wrote:
Is there no better way known to Roku of doing roAssociativeArray copy than the "manu-matic" way i show above?
(no intent to criticize here, just would like yes/no determination)
PS. why may one need that you ask? well since objects are emulated with roAssocArrays but there are no classes/constructors, one has to create objects by cloning prototype objects... a-la javascript
Why yes, there is - thanks for asking - use {} + .append() :
counter = {i: 0, next: function():m.i = m.i+1:return m.i:end function}
' clone it (shallow copy)
newObj = {}
newObj.append(counter)

TheEndless
Channel Surfer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-07-2014
11:23 AM
Re: How to clone roAssociativeArray?
You may be interested in this function I wrote for a bit more depth control and the ability to break references: viewtopic.php?f=34&t=57775&p=392701#p392575
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)