Finding a whole line
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Finding a whole line
I've got some data that looks like this:
2,8011171004
17,8011171604
4,8011170304
5,8011170204
What I want to do is find the first number from the second number. For example, if I look for '8011171604', get '17'.
I tried 'find' but that just puts a box around the number, I can't seem to do anything with it and I also tried using filter, but that just gives me nothing.
Any ideas?
2,8011171004
17,8011171604
4,8011170304
5,8011170204
What I want to do is find the first number from the second number. For example, if I look for '8011171604', get '17'.
I tried 'find' but that just puts a box around the number, I can't seem to do anything with it and I also tried using filter, but that just gives me nothing.
Any ideas?
Re: Finding a whole line
Hi Andy,
use LINEOFFSET!
Lets presume the numbers are already in a variable name "tNumbers"
...
put "8011171604" into toLookFor
## or this may already in another variable
put lineoffset(toLookFor,tNumbers) into tLine
if tLine = 0 then
answer "Not in list!"
else
answer item 1 of line tLine of tNumbers
## -> 17
end if
...
Best
Klaus
use LINEOFFSET!
Lets presume the numbers are already in a variable name "tNumbers"
...
put "8011171604" into toLookFor
## or this may already in another variable
put lineoffset(toLookFor,tNumbers) into tLine
if tLine = 0 then
answer "Not in list!"
else
answer item 1 of line tLine of tNumbers
## -> 17
end if
...
Best
Klaus
Re: Finding a whole line
Blimey, this is an odd language!!!
Thanks for that Klaus, I'm off to make this thing work!
Thanks for that Klaus, I'm off to make this thing work!
Re: Finding a whole line
Hi,
One more thing. Lineoffset also finds parts of lines. To make sure that you find a whole line, set the wholeMatches to true before calling the offset function. Note that the wholematches is set to false whenever your handler finishes.
Kind regards,
Mark
One more thing. Lineoffset also finds parts of lines. To make sure that you find a whole line, set the wholeMatches to true before calling the offset function. Note that the wholematches is set to false whenever your handler finishes.
Kind regards,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Re: Finding a whole line
Also, your efforts with filter.. Filter can be weird at first but once you get it its pretty straightforward.
With your supplied data, it appears that you were looking for any lines that contain the number 8011171604.
Put the data into a temp variable, then to use filter you need to understand how the wildcards work.
filter "8011171604" won't work because filter is looking at the whole line and so there is no match.
Since you want to match only the 2nd item you can use a wildcard to build up the string you're searching for.
filter "*,8011171604" The asterisk will match any number of characters up until it finds the next explicit match which is the comma. So, basically this means it will ALWAYs match up to the comma for every line. Then it matches the 8011171604 against the rest of the line.
If you were looking for partial matches in the 2nd part, you can add asterisks at strategic locations for wildcard matches.
So say you were looking for lines that start with 8011
filter "*,8011*" will do that.
Or if you're looking for all occurrences of 1117 anywhere in the 2nd item.
filter "*,*1117*" will do that.
Then theres the inclusion and exclusion parts of how filter works. If you "filter with "yourstringtofilterwith" it will Keep the matches. If you filter without, it will keep the non-matches.
So heres some sample code.
Of course having said all this, for your purpose lineoffset is the way to go.
With your supplied data, it appears that you were looking for any lines that contain the number 8011171604.
Put the data into a temp variable, then to use filter you need to understand how the wildcards work.
filter "8011171604" won't work because filter is looking at the whole line and so there is no match.
Since you want to match only the 2nd item you can use a wildcard to build up the string you're searching for.
filter "*,8011171604" The asterisk will match any number of characters up until it finds the next explicit match which is the comma. So, basically this means it will ALWAYs match up to the comma for every line. Then it matches the 8011171604 against the rest of the line.
If you were looking for partial matches in the 2nd part, you can add asterisks at strategic locations for wildcard matches.
So say you were looking for lines that start with 8011
filter "*,8011*" will do that.
Or if you're looking for all occurrences of 1117 anywhere in the 2nd item.
filter "*,*1117*" will do that.
Then theres the inclusion and exclusion parts of how filter works. If you "filter with "yourstringtofilterwith" it will Keep the matches. If you filter without, it will keep the non-matches.
So heres some sample code.
Code: Select all
put myList into tempVar -- since the data in the actual contain is changed put it into a temporary var
filter tempVar with "*,8011171604" -- filter the var
answer information tempVar -- based on sample data should now only contain the 17,... line.
Of course having said all this, for your purpose lineoffset is the way to go.
Mark wrote:Hi,
One more thing. Lineoffset also finds parts of lines. To make sure that you find a whole line, set the wholeMatches to true before calling the offset function. Note that the wholematches is set to false whenever your handler finishes.
Kind regards,
Mark
Re: Finding a whole line
You guys are amazing, thanks!
I use LiveCode only occasionally to manipulate data but each time I learn something new, great to know there's such a community around here.
I use LiveCode only occasionally to manipulate data but each time I learn something new, great to know there's such a community around here.