Forum Discussion

EnTerr's avatar
EnTerr
Roku Guru
14 years ago

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

  • "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" 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)