Semicolon in a string "0;0;0"

q){x where x <> "abc"} each ("abc";"def";"abc";"ghi")"""def""""ghi"q)

Could you filter the following list in a similar way?

q){x where x <> "0;0;0"} ("0;0;0";"0;0;1";"0;0;0"){x where x <> "0;0;0"}'lengthq))

i.e. how to retain “0;0;1”? why the 'length exception? does the semicolon in a string creates a list?

Hi Roman,

You could use except.

x except enlist “0;0;0”

Regards,
Paul

x except enlist "0;0;0"
Yes. This is a more direct expression of my intent. Thanks Paul.

you missed the `each in your “0;0;0” case:

q){x where x <> “0;0;0”} each (“0;0;0”;“0;0;1”;“0;0;0”)

“”

,“1”

“”

which might help explain the 'length problem - but you get a result you might not expect…

ta, jack.

you missed the `each in your “0;0;0” case:

q){x where x <> “0;0;0”} each (“0;0;0”;“0;0;1”;“0;0;0”)

“”

,“1”

“”

which might help explain the 'length problem - but you get a result you might not expect…

 
oops, indeed
strange..

Actually, I read the data from a file and here I posted what I thought would be an equivalent:

q)show f:read0 :stats/f69l7.csv"0;0;1""0;0;0""0;0;0""0;0;0""0;0;0""0;0;0""0;0;0""0;0;0""0;0;0""0;0;0""0;0;0""0;0;0""0;0;0""0;0;0""0;0;0""0;0;0""0;0;0""0;0;0""0;0;0""0;0;0""0;0;0"“0;0;0”..q){x where x <> “0;0;0”} each f{x where x <> “0;0;0”}'lengthq))\q)type f0hq)type (“0;0;0”;“0;0;1”;“0;0;0”)0h`

because the types I checked do match. And there I used “each”.

Later I understood the problem.
The file is big (~24MB) and I did not see all possible values from the head. But there are such rows as well:

"4222687844;190345654;1""4227490595;451919285;2""1725924692;4141106660;1""1040033209;1484765725;1""2695285692;3536595978;1"..
and these strings of course have different length. So using “except” is the right choice here.

Thank you, guys.

depending on what you are trying to do, and your data distribution, this may also be of interest
{(flip abc!x)where any x}("JJJ";";")0::stats/f69l7.csv``

{(flip abc!x)where any x}("JJJ";";")0::stats/f69l7.csv

It took time to understand how “any x” works here. Because x is a list of lists, I would not come up with this solution myself. Interesting.

Thanks Charles.

How about;

q)f:{(y;0#y)y~x}

Cheers,

Michael

Sorry, an example would be useful;

q)f:{(y;0#y)y~x}

q)f[“abc”;]each(“abc”;“def”;“abc”;“ghi”)

“”

“def”

“”

“ghi”

q)

q)f[“0;0;0”;]each(“0;0;0”;“0;0;1”;“0;0;0”)

“”

“0;0;1”

“”

q)

q)f[1;]each(2 2 1 3 1 2 1)

2
2
`long$()
3
`long$()
2
`long$()

q)f:{(y;0#y)y~x}

q)f[“abc”;]each(“abc”;“def”;“abc”;“ghi”)

Hi Michael.

Your solution was more like a challenge to me.

I recognized y~x as a match and f[“abc”;] as partial application.
I did not understand the (y;0#y) part right away but here it goes:
(y or nothing of y) selected by (0 or 1) that is a result of the match.
You use 0#y to retain the type (to keep the function polymorphic).

It works and is something to learn from. Thank you.

f:{@[y;where y~:x;0#]}

Yes, exactly :)



Sent from my iPhone

f:{@[y;where y~:x;0#]}

Thank you, Rolf.

I read about Protected Evaluation in Q for Mortals. The book mentions function in the first place @[f;…;…]. Also though I read that list indexing syntax is intentionally similar to the syntax of function application, it is interesting how you use it here. Your example gives a better perspective on @[…]: would-be usual “y where y~:x” looks like being split in two cases: success and failure.

Also, 0# is a useful idiom.

don’t confuse protected execution with functional amend

http://code.kx.com/wiki/JB:QforMortals/functions#Apply_.28.40.29

don’t confuse protected execution with functional amend

Oh, that’s different.. Calibrated my perception. Many thanks.

there are several places where q functions are effectively polymorphic on type

. and @ are two of them – functions in the first arg are one sense, data structures a different one

fwiw, my solution to your original question would be

q){x where not x~:“0;0;0”} (“0;0;0”;“0;0;1”;“0;0;0”)

“0;0;1”

q)