0

I've found in "Q Tips" a technique to preserve keys in a table. This is useful for restriction columns in the right table in lj for example, without re-applying a key. Using each:

q)show t:(`c1`c2!1 2;`c1`c2!3 4)!(`c3`c4`c5!30 40 50;`c3`c4`c5!31 41 51)
c1 c2| c3 c4 c5
-----| --------
1  2 | 30 40 50
3  4 | 31 41 51
q)`c3`c4#/:t
c1 c2| c3 c4
-----| -----
1  2 | 30 40
3  4 | 31 41

I’m trying to understand why it preserves a key part of the table t:

q){-3!x}/:t
'/:
  [0]  {-3!x}/:t
              ^

But in this case q doesn’t show how it treats each row of the keyed table.

So why is this syntax #/:t works in such a way for a keyed table? Is it mentioned anywhere in code.kx.com docs?

Upd1: I've found a case with # and keyed table on code.kx.com, but it is about selecting rows, not columns.

egor7
  • 4,678
  • 7
  • 31
  • 55

2 Answers2

1

If you view the keyed table as a dictionary (which it is) then it's no different to:

q)2*/:`a`b!1 2
a| 2
b| 4

or

q){x+1} each `a`b!1 2
a| 2
b| 3

The keys are retained when applying a function to each element of a dictionary. In your example the function being applied is to use take on a dictionary, e.g:

q)`c3`c4#first t
c3| 30
c4| 40

doing that for each row returns a list of dictionaries which is itself a table.

Also your other attempt would work as:

{-3!x}@/:t

so it's not unique to take #

terrylynch
  • 11,844
  • 13
  • 21
  • 1
    Thank you Terry for clarifying each of my concerns, even with appropriate usage of `/:` each right for `@` function call. – egor7 Aug 13 '20 at 16:00
1
{-3!x}/:t

each right needs two arguments so this wont work.

Since the table is keyed, it is treated as a dictionary. The each right iterates over the dictionary values and therefore ignores the keys of the main dictionary (= the keyed columns). To see what is happening it might help to see what happens when using each:

q)){-3!x} each t
c1 c2|
-----| --------------------
1  2 | "`c3`c4`c5!30 40 50"
3  4 | "`c3`c4`c5!31 41 51"