Filter Script & Patterns & Regex

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

chipsm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 199
Joined: Wed May 11, 2011 7:50 pm
Location: Southern California

Filter Script & Patterns & Regex

Post by chipsm » Mon Dec 11, 2017 5:22 pm

I realize that this is a fairly intensive subject, but I think that an extensive Tutorial or example(s) for this subject is needed.
I am not the most sophisticated user, but I spent a whole day trying to understand and explore this subject of filters.
I have made some headway but I still feel far short from understanding what is needed for this capability. I have been told to use special repeat loops to achieve this capability, and I have, but it seems that a one line script that seems to have tremendous capability is the way to go.
There seems to be differences between using "FILTER" "with" and "Without", "patterns" and "patterns with Regex" and can be very daunting.
The Livecode Dictionary has some information but it is far shy of the abilities of this function.
I have been to the Regex site and that information, being very extensive, is as clear as mud, especially concerning Livecodes ability to utilize Regex.
Last edited by chipsm on Mon Dec 11, 2017 5:36 pm, edited 1 time in total.
Clarence Martin

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

Re: Filter Script & Patterns & Regex

Post by dunbarx » Mon Dec 11, 2017 5:33 pm

Regex is like dataGrids. I use them but do not understand them.

Craig Newman

chipsm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 199
Joined: Wed May 11, 2011 7:50 pm
Location: Southern California

Re: Filter Script & Patterns & Regex

Post by chipsm » Mon Dec 11, 2017 5:41 pm

Just from my rudimentary understanding of this subject, this scripting can be very powerful if it is understood.
I too have somewhat avoided Datagrids for a similar reason.
I hope that the LiveCode team would be able to make this subject very clear. I know that someone there must have a clear understanding about this or it would not have been included as a function.
Clarence Martin

bogs
Posts: 2068
Joined: Sat Feb 25, 2017 10:45 pm

Re: Filter Script & Patterns & Regex

Post by bogs » Mon Dec 11, 2017 5:45 pm

Yah, it can be confusing. I'm not sure I have it all straight, although I can manage fairly simple things ok.

My understanding (so far) is that -
. "filter with" = what you want to specifically see in the filtered result.
. "filter without" = what you want to specifically drop in the list.
. patterns = is a matter of matching whatever pattern you specify.
. regex = regular expressions.

Some examples of with and without (not using a repeat loop) -

Code: Select all

   put the folders into tmpFldrList
   sort tmpFldrList ascending
// to get rid of hidden folders...
   filter tmpFldrList without ".*"

Code: Select all

   put the files into tmpFileList
   sort tmpFileList ascending
// to get rid of hidden files...
   filter tmpFileList without ".*"
// make sure only specific extensions show up, drop everything else...
   filter tmpFileList with "*[.mc - .rev - .livecode]"
Max's wiki has a pretty complete entry on it here, see if that helps. If your using loops, you might also check the entry for 'matchText' and 'matchChunk', since those target one line only at a time.
Image

chipsm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 199
Joined: Wed May 11, 2011 7:50 pm
Location: Southern California

Re: Filter Script & Patterns & Regex

Post by chipsm » Mon Dec 11, 2017 6:00 pm

I came upon this need because I needed to parse a tab delimited list of items.
I wanted to filter the list by checking on the 3rd tab item from my list.
I finally settled on this:
filter lines of (field) with "*" & tab &'*" & tab &"value*"
this will filter the list of lines based on the 3rd tabbed item for each line.
Clarence Martin

Klaus
Posts: 10284
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Filter Script & Patterns & Regex

Post by Klaus » Mon Dec 11, 2017 7:06 pm

chipsm wrote:
Mon Dec 11, 2017 6:00 pm
...
I finally settled on this:
filter lines of (field) with "*" & tab &'*" & tab &"value*"
this will filter the list of lines based on the 3rd tabbed item for each line.
Hint:
Don't forget that the asterisk * stands for ANY character, means also for a TAB,
so this will also filter lines based on the 4th, 5th etc. tabbed item, if any!

bogs
Posts: 2068
Joined: Sat Feb 25, 2017 10:45 pm

Re: Filter Script & Patterns & Regex

Post by bogs » Mon Dec 11, 2017 7:09 pm

Here is another link from Max's wiki that may help cover regex better. Least I found it easier to understand.
Image

chipsm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 199
Joined: Wed May 11, 2011 7:50 pm
Location: Southern California

Re: Filter Script & Patterns & Regex

Post by chipsm » Mon Dec 11, 2017 7:35 pm

klaus,
can you please expand on your reply?

>Don't forget that the asterisk * stands for ANY character, means also for a TAB,
so this will also filter lines based on the 4th, 5th etc. tabbed item, if any!

are the tabs delimited "one for one" for each tab in the filter line?
ie. "*" & tab &"*" & tab - would by item 1 , and tab to the next item or 2 tabs items?
Sorry if I seem dense about this.
Clarence Martin

Klaus
Posts: 10284
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Filter Script & Patterns & Regex

Post by Klaus » Mon Dec 11, 2017 7:51 pm

Hi Clarence,

the asterisk is just a placeholder for one or MORE characters of ANY kind! So this:
...
filter lines of (field) with "*" & tab &'*" & tab &"value*"
...

Will also leave something like this in the field:
...
whatever TAB some string TAB another string TAB and yet another string TAB some numbers12245 TAB value
...

Best

Klaus

chipsm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 199
Joined: Wed May 11, 2011 7:50 pm
Location: Southern California

Re: Filter Script & Patterns & Regex

Post by chipsm » Mon Dec 11, 2017 8:02 pm

Thanks Klaus.
Clarence Martin

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

Re: Filter Script & Patterns & Regex

Post by dunbarx » Mon Dec 11, 2017 9:42 pm

I was just trying to be cute with the "don't understand" thing.

That is not to say I understand.

But note that the filter command can be used without all that fancy regex/wildCard stuff. If you have a fid 1 with "a,b,c,d,e,f,g" and you:

Code: Select all

on mouseUp
   filter items of fld 1 without "b" into temp
   answer temp
end mouseUp
you get the string without the "b".

One can move slowly from there, adding filter complexity piece by piece. Use the asterisk wildcard like Klaus mentioned, until you have enough confidence to try your first real regex gadget,

Then check out the "replaceText" function.

Craig

chipsm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 199
Joined: Wed May 11, 2011 7:50 pm
Location: Southern California

Re: Filter Script & Patterns & Regex

Post by chipsm » Mon Dec 11, 2017 10:47 pm

Craig,
I have been there. It gets more complicated as you add more. Especially when you use the Regex phrase - things get complicated. This where more information and help is needed by many developers including myself.
This is the cloudy part.
Clarence Martin

bogs
Posts: 2068
Joined: Sat Feb 25, 2017 10:45 pm

Re: Filter Script & Patterns & Regex

Post by bogs » Tue Dec 12, 2017 12:33 am

Neither of the links I posted helped? The one about regex was particularly enlightening to me, and has all kinds of information about them.
Image

MaxV
Posts: 1535
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Filter Script & Patterns & Regex

Post by MaxV » Tue Dec 12, 2017 2:01 am

chipsm wrote:
Mon Dec 11, 2017 6:00 pm
I came upon this need because I needed to parse a tab delimited list of items.
I wanted to filter the list by checking on the 3rd tab item from my list.
I finally settled on this:
filter lines of (field) with "*" & tab &'*" & tab &"value*"
this will filter the list of lines based on the 3rd tabbed item for each line.

I'd use this:
########CODE to copy and paste#######
put the text of field test into myList
set itemdel to TAB
repeat for each line tLine in myList
if item 3 of tLine is "myValue" then
put tLine & return after newList
end if
end repeat
delete last char of newList
set the text of field test to newList
#####END OF CODE generated by http://tinyurl.com/j8xf3xq with livecode 9.0.0-dp-6#####
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w

jiml
Posts: 163
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: Filter Script & Patterns & Regex

Post by jiml » Wed Dec 13, 2017 7:26 pm

Clarence,

An alternative to FILTER for displaying only certain lines in a field is the HIDDEN property.

Code: Select all

set itemdel to TAB
lock screen
repeat with x = 1 to the number of lines of fld "myFld"
	if item 3 of line x of fld "myFld" is NOT "myValue" then
		set the HIDDEN of line x of fld "myFld" to true
	end if
end repeat
unlock screen
This is non-destructive. The original contents of the field are preserved.
You can restore the original display by:

Code: Select all

lock screen
repeat with x = 1 to the number of lines of fld "myFld"
	set the HIDDEN of line x of fld "myFld" to false
end repeat
unlock screen
Jim Lambert

Post Reply

Return to “Talking LiveCode”