# parameterized update

**URL:** <https://forum.kx.com/t/parameterized-update/6843>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [January 3, 2011, 4:19am UTC](https://forum.kx.com/t/parameterized-update/6843 "2011-01-03T04:19:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![effbiae](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@effbiae](https://forum.kx.com/u/effbiae)\
**Post date:** [January 3, 2011, 4:19am UTC](https://forum.kx.com/t/parameterized-update/6843/1 "2011-01-03T04:19:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Aaron\_Davies](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Aaron\_Davies](https://forum.kx.com/u/Aaron_Davies)\
**Post date:** [January 3, 2011, 1:21pm UTC](https://forum.kx.com/t/parameterized-update/6843/2 "2011-01-03T13:21:00Z")

</div>

https://code.kx.com/trac/wiki/Reference/Slash#over

(specifically the&nbsp;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

---

<div class="post-metadata">

**Author:** ![acs\_kdb](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@acs\_kdb](https://forum.kx.com/u/acs_kdb)\
**Post date:** [January 22, 2011, 10:39am UTC](https://forum.kx.com/t/parameterized-update/6843/3 "2011-01-22T10:39:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![effbiae](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@effbiae](https://forum.kx.com/u/effbiae)\
**Post date:** [January 23, 2011, 3:54am UTC](https://forum.kx.com/t/parameterized-update/6843/4 "2011-01-23T03:54:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Aaron\_Davies](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Aaron\_Davies](https://forum.kx.com/u/Aaron_Davies)\
**Post date:** [January 23, 2011, 7:15am UTC](https://forum.kx.com/t/parameterized-update/6843/5 "2011-01-23T07:15:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![acs\_kdb](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@acs\_kdb](https://forum.kx.com/u/acs_kdb)\
**Post date:** [January 23, 2011, 1:40pm UTC](https://forum.kx.com/t/parameterized-update/6843/6 "2011-01-23T13:40:00Z")

</div>

[http://en.wikipedia.org/wiki/Step\_function](http://en.wikipedia.org/wiki/Step%5C_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
