Reputation: 19466
Let's say I have
k: `a`b`c
v: 1 2 3
How can we make a string of the form:
a=1&b=2&c=3
Upvotes: 1
Views: 207
Reputation: 13572
This is very situational but if you're only doing this for "display" purposes then you could make use of built-in .Q.s
q)ssr/[-2_.Q.s d;("| ";"\r\n");("=";"&")]
"a=1&b=2&c=3"
The output would be limited by your console size though, so for all practical purposes you would/should use kylebonnes answer.
EDIT: .Q.s2
gets you pretty close too:
q)"&"sv"="^.Q.s2 d
"a|=1&b|=2&c|=3"
EDIT2: based on your further disclosure that the purpose of this is to build query strings then I've a few other comments:
,
instead of &
then. You shouldn't normally use &
in where clauses in queries.d:`a`b`c!`one`two`three
and you're trying to build a query that says
where a=`one,b=`two,c=`three
in this case you'd have to try to use maybe .Q.s1
or else have logic to put the backticks in place.
q)"?[t;",(-3!{(=;x;d x)}each key d),";0b;()]"
"?[t;((=;`a;1);(=;`b;2);(=;`c;3));0b;()]"
h:hopen`::1234;
h({<function to generate query without having to string>};`a`b`c!1 2 3)
Upvotes: 0
Reputation: 936
The following lambda accomplishes what you want (accepting a dictionary as its argument):
{"&" sv "=" sv/: string flip (key;value)@\:x} `a`b`c!1 2 3
Upvotes: 3