Page 1 of 1

Array key names

Posted: Mon Mar 08, 2010 2:58 am
by phaworth
What is the correct way to change the name of an array element? For example, how do I change myArray["xyz"] to be myArray["abc"].

Thanks,
Pete

Re: Array key names

Posted: Mon Mar 08, 2010 2:51 pm
by oliverk
Hi Pete,

I don't think there is a way to do this in a single line of code. I would do something like this:

Code: Select all

command arrayKeyRename @xArray, pKeyName, pNewKeyName
   local tKeyValue
   put xArray[pKeyName] into tKeyValue
   delete variable xArray[pKeyName]
   put tKeyValue into xArray[pNewKeyName]
end arrayKeyRename
The @ before the first parameter indicates that the array is passed by reference, this allows you to mutate it inside the command and is also faster than passing the value which may be quite large.

You can use it like this:

Code: Select all

on mouseUp
   local tArray
   put "test" into tArray["test1"]
   arrayKeyRename tArray, "test1", "test2"
   put tArray["test2"] --> test
end mouseUp
Hope this helps.

Regards
Oliver

Re: Array key names

Posted: Mon Mar 08, 2010 6:25 pm
by phaworth
Thanks Oliver. I was doing something very similar to what you suggested but hoped there might be a simpler way to do it.
Pete