kgf3JfUtW
kgf3JfUtW

Reputation: 14926

kdb: check whether a symbol starts with a particular prefix

Given a symbol, how to check whether it has a particular prefix?

I had below code. It checks if a symbol begins with aaaaa but returns 1b for aaa which is wrong. I can add a length check but that seems verbose. Is there a cleaner way?

{"aaaaa"~-5#string x}[`$"aaa"]

Upvotes: 1

Views: 1236

Answers (3)

CWD
CWD

Reputation: 353

Expanding on Matthew's answer if you want to make a function out of it do the following:

q)f:{x like "aaaaa*"}
q)f[`aaa]
0b
q)f[`aaaaa]
1b
q)f[`aaaaabcde]
1b

And if you want to make it more dynamic you could add a second variable for the matching prefix.

q)f2[`aaa;"aaa"]
1b
q)f2:{x like y,"*"}
q)f2[`aaa;"aaaaa"]
0b
q)f2[`aaa;"aaa"]
1b

Let me know if you see any issues.

Upvotes: 1

nordonez
nordonez

Reputation: 19

It seems like the issue is with "take" since "aaa" is shorter than 5. It's extending "aaa" by 2/3 of itself in order to meet that length.

You could modify your function so you have the following:

q){"aaaaa"~(x) til 5}["aaa"]
0b
q){"aaaaa"~(x) til 5}["aaaaaaaa"]
1b

Upvotes: 1

user12704155
user12704155

Reputation:

Could you use like?

q)`aaa like "aaa*"
1b 
q)`aaa like "aaaaa*"
0b

Upvotes: 4

Related Questions