Page 1 of 1

Searching array keys

Posted: Mon Apr 04, 2016 12:20 am
by bd525
Apologies if this question is easily answered. I just can't find it.

I am making use of simple arrays. I understand how to reference a variable in an array with the key: put thisArray[1] into thatVar. But what if I want to determine what key value is associated with a particular variable, say a text string? The keys are a sequence of integers; the variables are text strings. How do I determine what integer (key) goes with a given text string (variable)? I can make things work with repeat structures, but I'd prefer to be more sparing with code if possible.

I'm migrating from Director, and this is very simple using functions associated with property lists, the equivalent of arrays in LiveCode.

No doubt there is an easy answer, but Teh Google and searching the forums has not provided it yet.

Thanks!

Bruce

Re: Searching array keys

Posted: Mon Apr 04, 2016 1:37 am
by FourthWorld
bd525 wrote:I'm migrating from Director, and this is very simple using functions associated with property lists, the equivalent of arrays in LiveCode.
What would the code look like in Director?

Re: Searching array keys

Posted: Mon Apr 04, 2016 1:53 am
by bd525
put ["one":1,"two":2] into thisProp
put getaProp(thisProp,"two")
returns: 2

Re: Searching array keys

Posted: Mon Apr 04, 2016 2:28 am
by FourthWorld
Is 2 the value or the key? And what does it return when it finds multiple matches?

Re: Searching array keys

Posted: Mon Apr 04, 2016 3:01 am
by bd525
FourthWorld wrote:Is 2 the value or the key?
The key.
FourthWorld wrote:And what does it return when it finds multiple matches?
Sorry, not sure what you mean. In this case it's just as shown: A text string for the variable, an integer for the key. Sorry if my understanding of these concepts is not sophisticated. All I need to do in this case is the the reverse of a typical lookup for a variable value from a key value.

Also, no biggie; I can always find the value with a repeat structure if necessary. Was just wondering about how things work in LIveCode.

Re: Searching array keys

Posted: Tue Apr 05, 2016 1:47 am
by sritcp
bd525 wrote:
FourthWorld wrote:And what does it return when it finds multiple matches?
Sorry, not sure what you mean. ....
The keys of an array are unique. No two elements of an array can have the same key. In your example of numerical keys (1,2,3,...), there is only one element called myArray[2], for instance.
On the other hand, many elements can share the same value. For example, say, the value of myArray[2] is "thisString". There is nothing to prevent myArray[4] to have the same value. In fact, every single element of an array can have the same value, if you want it that way!
This makes reverse lookup a problem, if you are looking for a unique answer. Does "thisString" refer to myArray[2] or myArray[4]? Yes, and yes.

Regards,
Sri

Re: Searching array keys

Posted: Tue Apr 05, 2016 4:52 pm
by bd525
Thanks, that makes sense, and is helpful.

I've realized there is a simple solution to my problem. The simple arrays I need all have unique elements, so for those few arrays I need to do a "reverse look up" I simply create a mirror version, with keys and elements reversed. It works!

Thanks for the input.

Re: Searching array keys

Posted: Tue Apr 05, 2016 4:58 pm
by dunbarx
Hi.

Know that the keys of an array do not have to be integers, and likely rarely are. These are created by default unless you tell LC otherwise, which one usually does:

Code: Select all

put "A" into myArray["goodGrade"]
put "F" into myArray["badGrade"]
Craig Newman