Brozo
Brozo

Reputation: 337

q / KDB+ How to iterate through list of strings

I have recently began learning q programming language and I find it a little bit of a struggle since I am coming from the 'traditional' imperative, OOP, etc language background. I am trying to perform a seemingly simple task - find strings in list that contain specific character:

names:("Saint Denis";"Rhodes";"Strawberry";"Valentine")

How do I print all strings that contain letter "R"? Even better, both "R" and "r"?

The furthest I was able to get is this:

{[x]x?"o"} each names

What would be the correct approach?

Thanks in advance!

Upvotes: 1

Views: 1205

Answers (3)

Scott
Scott

Reputation: 324

Another couple of alternatives are:

q)names where "r" in'lower names
"Rhodes"
"Strawberry"

q)names where any"Rr"in'\:names
"Rhodes"
"Strawberry"

Upvotes: 2

Matthew Madill
Matthew Madill

Reputation: 880

You could use the like keyword with a regular expression like so:

q)names where names like "*[Rr]*"
"Rhodes"
"Strawberry"

Upvotes: 6

Adam Bonham
Adam Bonham

Reputation: 615

You could use something like the below

q)names where "R" in/: names
"Rhodes"
q)names where any each "rR" in/: names
"Rhodes"
"Strawberry"

Upvotes: 3

Related Questions