« Given a value, how do I get the null of that value's type? | Main | How can I experiment on a production kdb while keeping the data free from accidental damage? »

What are 0!, 1! and 2!?

When !'s left argument is a positive integer, it sets the key fields of a table:

q)flip `a`b`c ! (1 2 3; 1 2 3; 1 2 3)
a b c
-----
1 1 1
2 2 2
3 3 3
q)1! flip `a`b`c ! (1 2 3; 1 2 3; 1 2 3)
a| b c
-| ---
1| 1 1
2| 2 2
3| 3 3
q)2! flip`a`b`c ! (1 2 3; 1 2 3; 1 2 3)
a b| c
---| -
1 1| 1
2 2| 2
3 3| 3

When !'s left argument is zero, it returns an unkeyed table from a keyed table:

q)0! ([a: 1 2 3; b: 1 2 3]; c: 1 2 3)
a b c
-----
1 1 1
2 2 2
3 3 3
q)

An arguably more readable equivalent is the xkey function:

q)`a xkey flip `a`b`c ! (1 2 3; 1 2 3; 1 2 3)
a| b c
-| ---
1| 1 1
2| 2 2
3| 3 3
q)`a`b xkey flip `a`b`c ! (1 2 3; 1 2 3; 1 2 3)
a b| c
---| -
1 1| 1
2 2| 2
3 3| 3
q)() xkey ([a: 1 2 3; b: 1 2 3]; c: 1 2 3)
a b c
-----
1 1 1
2 2 2
3 3 3
q)

It's easy to confuse function xkey with key, keys, xcol and xcols.

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Textile formatting is allowed.