Finding a string in a list

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
thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 383
Joined: Mon Mar 01, 2010 7:13 pm
Contact:

Finding a string in a list

Post by thatkeith » Sun Oct 27, 2013 11:34 am

I have a string of numbers that I'd like to match against a list of different numbers. Currently the list is in a two-words-per-line form, with the first word being the 'thing to match' and the second word being a score that this combination gets, for example:

1111 1100
1115 1050
1155 300
1555 600
5555 550
1222 300

...and so on. So if my string of numbers is "1155" then the score is "300".

How should I go about comparing a number string against the list so that I can figure out what it scores? Is there a better way for me to organise the list - without getting too esoteric or complex? :)

Keith
Technical Writer, Meta
University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist

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

Re: Finding a string in a list

Post by Klaus » Sun Oct 27, 2013 12:27 pm

Hi Keith,

that's a simple on, use "lineoffset" :D

Code: Select all

...
put fld "your list with numbers" into tList
put "1155" & SPACE into tEntry
put lineoffset(tEntry,tList) into tLine
## No found!
if tLine = 0 then
   answer "not in list!"
   exit to top
end if
put word 2 of line tLine ot tLisl into the_second_number_in_list
## Do something with the found number -> the_second_number_in_list
...
Best

Klaus

tjm167us
Posts: 50
Joined: Sat Dec 03, 2011 8:27 pm

Re: Finding a string in a list

Post by tjm167us » Sun Oct 27, 2013 1:44 pm

I'm unsure how that list was constructed to begin with, however, I would probably opt to put it into an array, with the "key" being the first column, and the "value" being the second column.

Code: Select all

repeat for each line tLine in theList
put word 1 of tLine into theKey
put word 2 of tLine into theArray[theKey]
end repeat
Then, if you wanted to find out what score corresponded to 1115 you would simply do:

Code: Select all

answer theArray[1115]
One other thing to add, if you made that list (as a lookup table) manually, then instead of creating the list, then creating the array, I would just manually create the array like so:

Code: Select all

put 1115 into tArray[1110]
put 1120 into tArray[1111]
etc. (with your numbers of course)

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Finding a string in a list

Post by Thierry » Mon Oct 28, 2013 9:05 am

Hi,

If you find that using a regex is not too esoteric,
then this small script does what you want and accept empty lines.

Code: Select all

 if matchText( field 1, "(?m)^1155\s+(\d+)", theValue) then
      return theValue
being a bit more verbose:

Code: Select all

on mouseUp
   answer funWithArrayLike( 1155 )
end mouseUp


function funWithArrayLike theKey
   if matchText( field 1, "(?m)^" & theKey & "\s+(\d+)", theValue) then
      return theValue
   else
      return "not found"
   end if
end funWithArrayLike
Be happy,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 383
Joined: Mon Mar 01, 2010 7:13 pm
Contact:

Re: Finding a string in a list

Post by thatkeith » Fri Jan 03, 2014 6:14 pm

Heh. Great suggestions - stuff that'll keep my learning nice and curved too. ;)

Sorry I didn't get back to y'all on this, I got distracted with a few other LC projects. I'll try this again shortly. It's an attempt to make the dice game "5000", largely as a learning exercise. So I'll try all the above suggestions, I promise! :D

Keith
Technical Writer, Meta
University Lecturer
Technical Editor, MacUser (1996-2015)
360 VR media specialist

Post Reply