Page 1 of 1

Filter each lines on a item value

Posted: Tue Aug 10, 2021 10:30 am
by Zax
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.

Re: Filter each lines on a item value

Posted: Tue Aug 10, 2021 1:02 pm
by richmond62
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.

Re: Filter each lines on a item value

Posted: Tue Aug 10, 2021 2:18 pm
by Zax
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

Re: Filter each lines on a item value

Posted: Tue Aug 10, 2021 2:55 pm
by dunbarx
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

Re: Filter each lines on a item value

Posted: Tue Aug 10, 2021 3:30 pm
by Zax
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.

Re: Filter each lines on a item value

Posted: Tue Aug 10, 2021 4:02 pm
by stam
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)…

Re: Filter each lines on a item value

Posted: Tue Aug 10, 2021 5:23 pm
by dunbarx
Stam.

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

Craig :wink:

Re: Filter each lines on a item value

Posted: Tue Aug 10, 2021 6:18 pm
by jacque
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.

Re: Filter each lines on a item value

Posted: Tue Aug 10, 2021 7:42 pm
by stam
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 ;)

Re: Filter each lines on a item value

Posted: Tue Aug 10, 2021 8:03 pm
by dunbarx
Lazy? Hah. I invented lazy.

Craig

Re: Filter each lines on a item value

Posted: Tue Aug 10, 2021 8:03 pm
by AxWald
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!

Re: Filter each lines on a item value

Posted: Tue Aug 10, 2021 9:22 pm
by jacque
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.

Re: Filter each lines on a item value

Posted: Tue Aug 10, 2021 9:44 pm
by dunbarx
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

Re: Filter each lines on a item value

Posted: Wed Aug 11, 2021 9:25 am
by richmond62
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.

Re: Filter each lines on a item value

Posted: Wed Aug 11, 2021 9:32 am
by Zax
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.