Array question

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
kpeters
Posts: 112
Joined: Wed Mar 21, 2007 9:32 pm

Array question

Post by kpeters » Sat Jul 14, 2007 6:25 am

Assume an array where every key/value pair is unique.

Given the value of an array element, find the corresonding key.

Is there some nifty function for this that I as a newbie don't know or do I have to cycle through the entire array to find the match?

TIA,
Kai

Klaus
Posts: 14193
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post by Klaus » Sat Jul 14, 2007 8:29 am

Hi Kai,

sorry, no nifty function ;-)

But you could use a nifty trick using the "combine" function to extract the "key" from the array like this:

Given "tArry" is your array and "unique_value" is the value of the unknown key you are looking for.

...
combine tArray by CR and TAB
## This will turn your array into a CR separated list of
## "key TAB value" lines
put lineoffset(TAB & "unique_value",tArray) into tLine
set itemdel to TAB
put item 1 of line tLine of tArray into finally_found_the_fricking_key
...

Et voila, now you finally found the fricking key :-)

Hope that helps.


Best from germany

Klaus

kpeters
Posts: 112
Joined: Wed Mar 21, 2007 9:32 pm

Post by kpeters » Sat Jul 14, 2007 5:41 pm

Thanks Klaus ~

that'll work just great for me!

Kai

Klaus
Posts: 14193
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post by Klaus » Sun Jul 15, 2007 2:25 pm

Hi Kai,

since Rev introduced "repeat for each key tKey in tArray" in version 2.7.2,
maybe a repeat loop is a bit faster nevertheless?


Best

Klaus

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sun Jul 15, 2007 3:39 pm

Hi Klaus and Kai,

Wouldn't that be "repeat for each element" in this case"

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Klaus
Posts: 14193
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post by Klaus » Sun Jul 15, 2007 4:59 pm

Mark wrote:Hi Klaus and Kai,

Wouldn't that be "repeat for each element" in this case"

Best,

Mark
Hi Mark,

can you retrieve the key with this? Don't think so.
Kai needs to get the "unknown" key from a "known" value.


Best

Klaus

xApple
Posts: 113
Joined: Wed Nov 29, 2006 10:21 pm

Post by xApple » Sun Jul 15, 2007 5:07 pm

Code: Select all

function getKeyOfValue theArray, theValue
  repeat for each line currentKey in the keys of theArray 
    if theArray[currentKey] = theValue then return currentKey 
  end repeat 
end getKeyOfValue

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sun Jul 15, 2007 6:01 pm

xApple's solution is fine, but just to show that it can be done with elements:

Code: Select all

on mouseUp
  -- make an array
  repeat with x = 1 to 10
    put numtochar(x+96) into myArray["k" & x]
  end repeat
  -- search for key of letter f
  put 0 into myCounter
  repeat for each element myElement in myArray
    add 1 to myCounter
    if myElement is "f" then exit repeat
  end repeat
  put line myCounter of the keys of myArray
end mouseUp
The advantage of this would be that you don't need to look up the element after getting the key first. I don't know what is faster though.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Klaus
Posts: 14193
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post by Klaus » Sun Jul 15, 2007 6:29 pm

Hi all,

and again we proved that there are many rev-ways to skin a rev-cat :-)

Post Reply