Page 1 of 1
Finding a string in a list
Posted: Sun Oct 27, 2013 11:34 am
by thatkeith
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
Re: Finding a string in a list
Posted: Sun Oct 27, 2013 12:27 pm
by Klaus
Hi Keith,
that's a simple on, use "lineoffset"
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
Re: Finding a string in a list
Posted: Sun Oct 27, 2013 1:44 pm
by tjm167us
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:
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)
Re: Finding a string in a list
Posted: Mon Oct 28, 2013 9:05 am
by Thierry
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
Re: Finding a string in a list
Posted: Fri Jan 03, 2014 6:14 pm
by thatkeith
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!
Keith