Searching array keys
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Searching array keys
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9838
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Searching array keys
What would the code look like in Director?bd525 wrote:I'm migrating from Director, and this is very simple using functions associated with property lists, the equivalent of arrays in LiveCode.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Searching array keys
put ["one":1,"two":2] into thisProp
put getaProp(thisProp,"two")
returns: 2
put getaProp(thisProp,"two")
returns: 2
-
- VIP Livecode Opensource Backer
- Posts: 9838
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Searching array keys
Is 2 the value or the key? And what does it return when it finds multiple matches?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Searching array keys
The key.FourthWorld wrote:Is 2 the value or the key?
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.FourthWorld wrote:And what does it return when it finds multiple matches?
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
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.bd525 wrote:Sorry, not sure what you mean. ....FourthWorld wrote:And what does it return when it finds multiple matches?
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
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.
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.
-
- VIP Livecode Opensource Backer
- Posts: 9665
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Searching array keys
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:
Craig Newman
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"]