Reputation: 519
I am trying to replace two values in a column in a table. But some records do not have both values. The whole column type is string.
This is the code I have, the column look like:
a:([]extraData:("xxx=xxxx;bbbb=bbbb;client=xxxx;Account=oooo;ffaijo=efaie";"xxx=xxxx;bbbb=bbbb;Account=oooo;ffaijo=efaie";"xxx=xxxx;bbbb=bbbb;client=xxxx;ffaijo=efaie";"xxx=xxxx;bbbb=bbbb;ffaijo=efaie"))
update {";"sv @[s;where (s:";"vs x) like "client=*";:;enlist""]}each extraData from (update {";"sv @[s;where (s:";"vs x) like "Account=*";:;enlist""]}each extraData from a)
a is the table. Some records do not have client or account and the code breaks. I am trying to use condition like
$["*client*" in a`extraData;;]
but do not work.
So my goal is to delete those two values. If just one, delete that one if both delete both.
Upvotes: 0
Views: 1050
Reputation: 461
THIS
update {";"sv s@(til count s)except where(s:";"vs x)like\:"client=*"}'[extraData]from a
Upvotes: 0
Reputation: 2268
I would start by writing a pair of functions to convert your strings to mappings and back:
ms:(!)."S=;"0:
sm:{";"sv"="sv/:flip(string key x;value x)}
On your data, it works like this:
q)ms "xxx=xxxx;bbbb=bbbb;client=xxxx;Account=oooo;ffaijo=efaie"
xxx | "xxxx"
bbbb | "bbbb"
client | "xxxx"
Account| "oooo"
ffaijo | "efaie"
q)sm ms "xxx=xxxx;bbbb=bbbb;client=xxxx;Account=oooo;ffaijo=efaie"
"xxx=xxxx;bbbb=bbbb;client=xxxx;Account=oooo;ffaijo=efaie"
Now, with
a:([]extraData:enlist "xxx=xxxx;bbbb=bbbb;client=xxxx;Account=oooo;ffaijo=efaie")
you can do the update as follows:
q)update sm each upsert[;`client`Account!("yyyy";"pppp")] each ms each extraData from a
extraData
----------------------------------------------------------
"xxx=xxxx;bbbb=bbbb;client=yyyy;Account=pppp;ffaijo=efaie"
Upvotes: 4
Reputation: 1692
You could add an if statement in to check if the necessary key exists (probably a nicer solution out there):
q)tab:([]extraData:("xxx=xxxx;bbbb=bbbb;client=xxxx;Account=oooo;ffaijo=efaie";"xxx=xxxx;bbbb=bbbb;Account=oooo;ffaijo=efaie"))
q)update {if[not x like "*client*";:x];";"sv @[s;where (s:";"vs x) like "client=*";:;enlist""]}each extraData from tab
extraData
-----------------------------------------------
"xxx=xxxx;bbbb=bbbb;;Account=oooo;ffaijo=efaie"
"xxx=xxxx;bbbb=bbbb;Account=oooo;ffaijo=efaie"
Upvotes: 2