Array key names

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Array key names

Post by phaworth » Mon Mar 08, 2010 2:58 am

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

oliverk
Site Admin
Site Admin
Posts: 53
Joined: Mon Feb 27, 2006 2:16 pm
Location: Edinburgh

Re: Array key names

Post by oliverk » Mon Mar 08, 2010 2:51 pm

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
Oliver Kenyon
Software Developer
Runtime Revolution

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: Array key names

Post by phaworth » Mon Mar 08, 2010 6:25 pm

Thanks Oliver. I was doing something very similar to what you suggested but hoped there might be a simpler way to do it.
Pete

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”