hi helpful folk,here’s some generated data (t) and an updatefunction that shows what i’m trying to do.its ugly, because i can’t make this sort ofoperation ‘functional’ (ie - return a valuerather than modifying t)can you show me how to pass (t) rather than(
t) to some quasi-equivalent expression?q)t:flip a
b!flip 3 2# til 4;ta b—0 12 30 1q){update a:x 1 from y where b
https://code.kx.com/trac/wiki/Reference/Slash#over
(specifically the f/[x;y] form)
q)t{[x;y]update a:y 1 from x where b<y 0}/(4 3;3 2)
a b
2 1
3 3
2 1
Looks like you’re trying to perform a mapping based on a step function.
If the number of entries in the step function is large, the multiple
update passes will not be performant.
So maybe you want something like:
t2:(a:2 3;b:3 4) / b must be in ascending order
update a:t2[a][1+t2[
b]bin b]from t / the 1+ in there for
“strictly less than”
But if I’m reading too much into the question, Aaron’s answer would be
more applicable.
ACS
thanks acs,
nice code.? so i’ve now learned about the bin
function and also this use of another table in
an update clause.
to shorten it, i guess i could use a list?
v:(2 3;3 4)?
update a:v[0][1+v[1]bin b]from t
but i think you were being kind to me to put
it in table form :)
ta, jack.
anything that evaluates to a list (of appropriate type and length) will work in an update–t2[a] is just another way of saying "t2.a", "exec a from t2", "(flip t2)
a", etc.
btw this could also be written in “txf” form (addressing a table by row number (or column(s)) first, then by column name:
q)t2:(a:2 3;b:3 4)
q)update a:t2[1+t2[b]bin b;
a]from t
a b
2 1
3 3
2 1
if you want step function behavior, the best option imho is using `s# dictionaries/keyed tables:
q)t2:`s#([b:1 3]a:2 3)
q){y,'x y}[t2]select b from t
b a
1 2
3 3
1 2
http://en.wikipedia.org/wiki/Step\_function
“Informally speaking, a step function is a piecewise constant function
having only finitely many pieces.”
The semantics between “bin” and “?” (the latter being used by keyed
table lookup) are different if one wants to specify intervals rather
than every possible point. This likely to be the case with floats.
Think of a table specifying tiered pricing:
q)t2:(a:6 4 2f;b:1 10 100f)
q)t2[a][t2[
b]bin 10f] / using <=3D semantics in this example, so
leaving off the 1+
4f
q)t2[a][t2[
b]bin 20f]
4f
q)t2k:b xkey t2 q)t2k[10f;
a]
4f
q)t2k[20f;`a]
0n
The original problem stated the relationship in terms of “less than”
rather than “equals”, so I assumed that the example data was not
representative.
ACS