0

Let's say I have the following,

q)add2:{[x]:x+2};
q)Bidcols:`Bid1px`Bid2px`Bid3px;
q)table:([]time:9 11;Bid1px:4 5;Bid2px:7 3;Bid3px:6 8);
time Bid1px Bid2px Bid3px
-------------------------
9    4      7      6
11   5      3      8

and I want to apply this add2 function to each cols of the table like the below

q)table:update Bid1px:add2'[Bid1px],Bid2px:add2'[Bid2px],Bid3px:add2'[Bid3px] from table;
time Bid1px Bid2px Bid3px
-------------------------
9    6      9      8
11   7      5      10

My questions are:

  1. Is there a way to do this using Bidcols?
  2. What are the other efficient ways to achieve this?

Thanks in advance.

1
  • As my answer to your previous question suggested (stackoverflow.com/a/68919735/3895697), whenever you want to parameterise a select statement you should consider switching to the functional form. Commented Aug 30, 2021 at 9:40

3 Answers 3

3

You can do this using a function select:

q)?[table; (); 0b; `time`Bid1px ! (`time; (each; add2; `Bid1px))]
time Bid1px
-----------
9    6     
11   7    

For (1), if you want to do it using Bidcols:

q)?[table; (); 0b; ] (cols table) ! {$[x in Bidcols; (each; add2; x); x]} each cols table
time Bid1px Bid2px Bid3px
-------------------------
9    6      9      8     
11   7      5      10  

I'm not sure what you mean for (2)? Are you asking for the most efficient way to do this?

Sign up to request clarification or add additional context in comments.

1 Comment

yes, are there any better ways to do this?
3

Functional select/update are most general and flexible approaches. However, in this particular case reassignment works well:

table[Bidcols]: add2 table[Bidcols];

because add2 function already supports vectors.

If add didn't support vectors straightaway, e.g.

add: {[x]: $[x>10;x+2;x+3]}

Following reassignment would work:

table[Bidcols]: (add'') table[Bidcols];

Comments

0

Another solution:

q)@[table;Bidcols;add2]
time Bid1px Bid2px Bid3px
-------------------------
9    6      9      8
11   7      5      10

or

q)@[`table;Bidcols;add2]

for an in-place update.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.