Finding a whole line

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
Andycal
Posts: 144
Joined: Mon Apr 10, 2006 3:04 pm

Finding a whole line

Post by Andycal » Tue Jan 31, 2012 4:16 pm

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?

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

Re: Finding a whole line

Post by Klaus » Tue Jan 31, 2012 4:22 pm

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

Andycal
Posts: 144
Joined: Mon Apr 10, 2006 3:04 pm

Re: Finding a whole line

Post by Andycal » Tue Jan 31, 2012 4:25 pm

Blimey, this is an odd language!!!

Thanks for that Klaus, I'm off to make this thing work!

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Finding a whole line

Post by Mark » Tue Jan 31, 2012 4:36 pm

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
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

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Finding a whole line

Post by sturgis » Tue Jan 31, 2012 5:23 pm

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.

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

Andycal
Posts: 144
Joined: Mon Apr 10, 2006 3:04 pm

Re: Finding a whole line

Post by Andycal » Tue Jan 31, 2012 5:45 pm

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.

Post Reply