AFox
AFox

Reputation: 97

How do you filter or search a nested list in q

Say I have a list

list:(`a`b;`ac`d;`e`af)

and I want to search for all symbols like a to give

(`a;`ac;`af)

How would I go about it?

If I try and use

list where each list like "*c*" 

(which without each works for a flat list) I get a type error

Thank you

Upvotes: 1

Views: 868

Answers (2)

Sean O'Hagan
Sean O'Hagan

Reputation: 1697

As you are looking for any occurrence and it seems you don't care about the position, you can just use raze here. It gives you your desired results.

q)list:(`a`b;`ac`d;`e`af)
q){r where (r:raze x) like y}[list;"a*"]
`a`ac`af

Upvotes: 2

Ryan McCarron
Ryan McCarron

Reputation: 909

Your approach is almost there - like works fine with flat lists, but needs to be told to operate on each element of the left argument:

q)list like\:"*c*"
00b
10b
00b

you can then use where each and apply back to each element of the initial list with each-both ('):

q)list@'where each list like\:"*c*"
`symbol$()
,`ac
`symbol$()

This returns only the elements matching the like right argument in each element, so there are empty lists returned.

Upvotes: 5

Related Questions