Filter each lines on a item value

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
Zax
Posts: 457
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Filter each lines on a item value

Post by Zax » Tue Aug 10, 2021 10:30 am

Hello,

I'm trying to filter lines of a variable testing an item.
Lets suppose tList is:

Code: Select all

abvc [TAB] 1 [TAB] hdchege
eekehek [TAB] 0 [TAB] lmlfevkvlke
lkopkj [TAB] 1 [TAB] kkekekemekc
I'm interested only by lines where item 2 of each is 1, so I wrote:

Code: Select all

set itemDelimiter to tab
filter lines of tList where item 2 of each = 1 
But the variable it doesn't contain anything.
Of course, I could do the same thing with a loop but, as filtering is optional, it will take longer (I would have to test filtering on each lines, or write 2 loops).

Thank you.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Filter each lines on a item value

Post by richmond62 » Tue Aug 10, 2021 1:02 pm

Well, for starters:

"abvc [TAB] 1 [TAB] hdchege" item 2 does NOT equal 1: it equals SPACE + 1 + SPACE

so start using 'contains'. 8)

ALSO, what does [TAB] mean?

what you posted as code does not look like code . . .

"abvc 1 hdchege" will work.

Zax
Posts: 457
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: Filter each lines on a item value

Post by Zax » Tue Aug 10, 2021 2:18 pm

richmond62 wrote:
Tue Aug 10, 2021 1:02 pm
what does [TAB] mean?
Sorry if I wasn't clear: "[TAB]" means a tab character (ASCII 9). So my example can be read as:
abvccharToNum(9)1charToNum(9)hdchege
eekehekcharToNum(9)0charToNum(9)lmlfevkvlke
lkopkjcharToNum(9)1charToNum(9)kkekekemekc

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

Re: Filter each lines on a item value

Post by dunbarx » Tue Aug 10, 2021 2:55 pm

Hi.

This works:

Code: Select all

on mouseUp
   repeat with y  = 1 to 3
    put "A" & tab & y & tab & "B" into line y of temp
   end repeat
   set the itemDel to tab
   
   filter lines of temp where item 2 of each <> 2
end mouseUp
Not that it matters in this case, but "lines" is the default for the filter command, so it is superfluous.

Craig

Zax
Posts: 457
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: Filter each lines on a item value

Post by Zax » Tue Aug 10, 2021 3:30 pm

Thanks Craig, it works :)

I realize that I didn't understand the Dictionary, where it is said:
If the filter command is used on a filterSource which is not a container, and no targetContainer is specified, the filtered string or array will be placed in the it variable.

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Filter each lines on a item value

Post by stam » Tue Aug 10, 2021 4:02 pm

Zax wrote:
Tue Aug 10, 2021 2:18 pm
richmond62 wrote:
Tue Aug 10, 2021 1:02 pm
what does [TAB] mean?
Sorry if I wasn't clear: "[TAB]" means a tab character (ASCII 9). So my example can be read as:
abvccharToNum(9)1charToNum(9)hdchege
eekehekcharToNum(9)0charToNum(9)lmlfevkvlke
lkopkjcharToNum(9)1charToNum(9)kkekekemekc
Also, and sorry if I misunderstand, there is no need to either use numToCodepoint(9) or describe it as [tab] - tab is a keyword and you can use it as is in your code and it will be understood as numToCodepoint(9)…

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

Re: Filter each lines on a item value

Post by dunbarx » Tue Aug 10, 2021 5:23 pm

Stam.

It matters little, but "tab" is a constant.

Craig :wink:

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7229
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Filter each lines on a item value

Post by jacque » Tue Aug 10, 2021 6:18 pm

I'm pretty sure [TAB] was meant just as an indicator here in the forum where typing the actual character could be mistaken for a space. I do the same thing.

Off the top of my head, this should also work and changes the variable itself:

Code: Select all

filter lines of tList with "*[TAB]1[TAB]* 
Where [TAB] is the actual typed character.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Filter each lines on a item value

Post by stam » Tue Aug 10, 2021 7:42 pm

jacque wrote:
Tue Aug 10, 2021 6:18 pm
I'm pretty sure [TAB] was meant just as an indicator here in the forum where typing the actual character could be mistaken for a space. I do the same thing.
It matters little, but...
Zax wrote:
Tue Aug 10, 2021 2:18 pm
Sorry if I wasn't clear: "[TAB]" means a tab character (ASCII 9)
@Craig - i stand corrected; i really meant 'reserved word' but got lazy ;)

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

Re: Filter each lines on a item value

Post by dunbarx » Tue Aug 10, 2021 8:03 pm

Lazy? Hah. I invented lazy.

Craig

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Filter each lines on a item value

Post by AxWald » Tue Aug 10, 2021 8:03 pm

Hi,

may be a mistake by myself, but too often I got the "filter" command to return things I'd not meant it to.
So I just stopped using it. Instead I do:

Code: Select all

   set itemdel to tab
   repeat for each line L in myData
     if (item 2 of L = 1) then put L & CR after myVar
   end repeat
   delete char -1 of myVar 
MyVar is now the "filtered" myData. Always, reliable & very fast.

Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7229
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Filter each lines on a item value

Post by jacque » Tue Aug 10, 2021 9:22 pm

Since you cant type a tab into the script editor, I revised my original suggestion:

Code: Select all

put "*" & tab & "1" & tab & "*" into tFilter
filter tList with tFilter
This works on your sample list, but I had to remove the spaces around the tabs. I assume your real list has no spaces. If it does, just add a space before and after the "1" in the filter string.

I also noticed when I copied your example that there are several invisible characters in the text. They may be due to the forum formatting, but check. You'll want to remove those, since the filter command will see them and nothing will match.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: Filter each lines on a item value

Post by dunbarx » Tue Aug 10, 2021 9:44 pm

too often I got the "filter" command to return things I'd not meant it to.
@Axwald. The filter command is on the same road that regex is. In fact, I suspect the two of collusion.

I have used filter successfully, but always seem to struggle a bit to get it right. I also feel more comfortable with a "filtering' loop. The cost is speed, but that said, a loop has more power and flexibility, especially in being able to work multiple "filters" in a single line of code.


Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Filter each lines on a item value

Post by richmond62 » Wed Aug 11, 2021 9:25 am

I'm pretty sure [TAB] was meant just as an indicator here in the forum
Quite possibly, but some newcomer might have taken it as code.

Craig, you may be 'lazy', but I am 'loopy' and always favour a conditional loop.

Zax
Posts: 457
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: Filter each lines on a item value

Post by Zax » Wed Aug 11, 2021 9:32 am

jacque wrote:
Tue Aug 10, 2021 9:22 pm
This works on your sample list, but I had to remove the spaces around the tabs. I assume your real list has no spaces. If it does, just add a space before and after the "1" in the filter string.
Right, Jacqueline.
I didn't know tab was allowed in the forum editor.

@AxWald : variable is duplicated in your example. Ii could be interesting to compare processing time with large list.
Also, I wanted to use the Filter command because I find it very elegant. This is a good example of the power of LC, IMHO.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”