Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
EnTerr
Level 11

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
Roku Employee
Roku Employee

Re: How to clone roAssociativeArray?

0 Kudos
EnTerr
Level 11

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
Level 11

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
Level 9

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