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: 
EnTerr
Roku Guru

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
0 Kudos
4 REPLIES 4
RokuJoel
Binge Watcher

Re: How to clone roAssociativeArray?

0 Kudos
EnTerr
Roku Guru

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
0 Kudos
EnTerr
Roku Guru

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)
0 Kudos
TheEndless
Channel Surfer

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)
0 Kudos