Searching array keys

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
bd525
Posts: 21
Joined: Sun Aug 31, 2014 12:43 am

Searching array keys

Post by bd525 » Mon Apr 04, 2016 12:20 am

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6713
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Searching array keys

Post by FourthWorld » Mon Apr 04, 2016 1:37 am

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?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

bd525
Posts: 21
Joined: Sun Aug 31, 2014 12:43 am

Re: Searching array keys

Post by bd525 » Mon Apr 04, 2016 1:53 am

put ["one":1,"two":2] into thisProp
put getaProp(thisProp,"two")
returns: 2

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6713
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Searching array keys

Post by FourthWorld » Mon Apr 04, 2016 2:28 am

Is 2 the value or the key? And what does it return when it finds multiple matches?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

bd525
Posts: 21
Joined: Sun Aug 31, 2014 12:43 am

Re: Searching array keys

Post by bd525 » Mon Apr 04, 2016 3:01 am

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.

sritcp
Posts: 368
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: Searching array keys

Post by sritcp » Tue Apr 05, 2016 1:47 am

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

bd525
Posts: 21
Joined: Sun Aug 31, 2014 12:43 am

Re: Searching array keys

Post by bd525 » Tue Apr 05, 2016 4:52 pm

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6068
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Searching array keys

Post by dunbarx » Tue Apr 05, 2016 4:58 pm

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

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”