Parsing type "C" column for specific value using a given pattern in kdb+

Viewed 44

Consider a column of type C in a kdb+ table that contains a message as follows:

They evaulated VAR_1(0.1)*VAR_2(0.2)*VAR_3(0.3)*VAR(1.1) > ? a*b

The objective is to look for the value specifically within VAR's parenthesis. Given the above example, the expectation is to return 1.1.

Here is my current unsuccessful attempt:

parseMsg:{l:{"VAR(" vs x} each ") >" vs x; l2:{x where {x[0] like "*VAR*"} each x} l1; "F"$12[0][1]};

I would then use the above to call select parseMsg each msg from tbl. While this runs, I'm not yet able to get the value within.

Any suggestions?

1 Answers

Your version works for me when you fix the typos:

t:([]msg:("They evaulated VAR_1(0.1)*VAR_2(0.2)*VAR_3(0.3)*VAR(1.1) > ? a*b";"They evaulated VAR_1(0.1)*VAR_2(0.2)*VAR_3(0.3)*VAR(22.22) > ? a*b"));

parseMsg:{l:{"VAR(" vs x} each ") >" vs x; l2:{x where {x[0] like "*VAR*"} each x} l; "F"$l2[0][1]};

q)select parseMsg each msg from t
msg
-----
1.1
22.22

You might also be able to use the 0: key-value shortcut https://code.kx.com/q/ref/file-text/#key-value-pairs

q)select{"F"$((!)."S()"0:x)`$"*VAR"}each msg from t
msg
-----
1.1
22.22
Related