help with filter command

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
slindstrom
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 29
Joined: Mon Apr 09, 2012 8:53 pm

help with filter command

Post by slindstrom » Tue Jul 23, 2019 11:47 pm

Hello. I find myself at a loss in using the filter command.

My instant problem: I have a tab-delimited list in a variable and wish to filter out all lines containing instances of the string tab & "abc" & tab.

I've tried this with no success.

put tab & "abc" & tab into cutString
filter lines of myVar without cutString

I've also tried this, also with no success.

filter lines of myVar without tab & "abc" & tab

Suspect I need to use a regex pattern but regex is mostly a foreign language to me.

How should I write a command to filter out lines containing that pattern?

I'm confounded. Thanks in advance!

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: help with filter command

Post by Xero » Wed Jul 24, 2019 1:03 am

do you want to filter lines that have "abc"?
What you have is a tab delimited string, so effectively, the tab doesn't exist (to what is reading the list). That being, what is being looked for is "abc", not "[tab] abc [tab]". The tab is not part of the data, it's just a separator.
Try filtering by "abc" alone (without the tab). If abc exists within other entries (i.e. abc, abcd, zabc), it should still look for "abc" only unless you use a wildcard structure (check documentation).
The other way around it may be to set your itemdelimiter to "comma" and filter tab & "abc" & tab, which would make 'tab' a part of the data and therefore searchable. Just remember to reset your itemdelimiter to tab afterwards.
Hope that helps.
X

slindstrom
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 29
Joined: Mon Apr 09, 2012 8:53 pm

Re: help with filter command

Post by slindstrom » Wed Jul 24, 2019 1:36 am

Thank you Xero. I hear what you're saying… Sort of. The LC replace command will handle a string consisting of tab & "abc" & tab.

And this works.
put tab & "abc" & tab into findString
get lineOffset(findString) in myVar

So I don't understand why the filter command can't accept tab as part of a string.

True, I can do something like this.

repeat forever
get lineOffset(findString) in myVar
if it > 0 then
delete line it of myVar
else
exit repeat
end if
end repeat

But I assume the snippet above would run much slower than if "filter regex pattern" could be used with tab as part of the regex expression.

filtering "abc" without the tabs isn't practical. Too many other instances of "abc" (just an example string).

I'll try your suggestion of setting itemdelimiter to comma… but don't quite follow how that will change anything since I'm filtering lines, not items. Do appreciate the suggestion.

If anybody can point me to using a regex pattern and filter for this, I'd be most grateful.

slindstrom
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 29
Joined: Mon Apr 09, 2012 8:53 pm

Re: help with filter command

Post by slindstrom » Wed Jul 24, 2019 1:41 am

Alas this doesn't help either.

"The other way around it may be to set your itemdelimiter to "comma" and filter tab & "abc" & tab, which would make 'tab' a part of the data and therefore searchable. Just remember to reset your itemdelimiter to tab afterwards."

:(

slindstrom
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 29
Joined: Mon Apr 09, 2012 8:53 pm

Re: help with filter command

Post by slindstrom » Wed Jul 24, 2019 2:02 am

And if I replace tab & "abc" & tab with "#abc#" in myVar and then…

filter lines of myVar without "#abc#"

that doesn't work either. So there is something I am fundamentally missing about how the filter command is supposed to be used. Help?

Thanks in advance,

Sieg

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: help with filter command

Post by SparkOut » Wed Jul 24, 2019 4:06 am

You certainly can filter with tabs in place.

When you are filtering lines without "your-filter-string" the filter string is matched against the whole line, so unless the entire content of your lines might be "<tab>abc<tab>" then your filter fails.

So you need to add wildcards to have the filter match a line with possible content before or after the filter string

Code: Select all

   put "*" & tab & "abc" & tab & "*" into tFilter
   filter lines of tVar without tFilter

slindstrom
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 29
Joined: Mon Apr 09, 2012 8:53 pm

Re: help with filter command

Post by slindstrom » Wed Jul 24, 2019 5:50 am

Bingo. Thanks so much, SparkOut!

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: help with filter command

Post by SparkOut » Wed Jul 24, 2019 7:40 am

You may find that you need to adjust the wildcards and placement in the filter if you have certain patterns to keep or exclude, such as only the start or end of a line.

Regex filters are possible too, but can be arcane, unless you have a handy Thierry. Not necessary in this case.

Starting with LC v9.5 you also get the option to filter ... where <expression> and do some calculations on (say) item 4 and 5 of each line, so if the multiply together and have a product less than han 200, filter those lines out. Not necessary in this case, but a valuable additional feature to the filter function.

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

Re: help with filter command

Post by dunbarx » Wed Jul 24, 2019 4:44 pm

Just to be clear, as Sparkout asserted, the filter command sees a tab as an ordinary ASCII character, just as it sees, say an "e". It is as valid a character as any other; it is indeed a member of a string.

The itemDelimiter doesn't enter into it, in the sense that it "separates" any part of a string from any other part in any special way.

Fileter

Post Reply

Return to “Talking LiveCode”