cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with xkey

MN_12
New Contributor III

Hi,

 

I am learning about tables for the first time and have been working through both the Tables and Tables Exercises notebooks.

I encountered a strange occurrence with xkey which I cannot figure out the cause of.

 

example is:

a b       c           
----------------------
1 mini    3.14        
2 example 2.72        
3 table   2.997925e+08

complexTab is:

c   d      
-----------
1 2 more   
3 4 complex
5 6 table  

The question (in the Tables notebook) asks us to create an unkeyed table called bigExample which combines both the tables example and complexTab to have the column order c d a b c.

I am unable to figure out why show bigExample:() xkey complexTab!example results in column c in complexTab taking the place of what should be column c  in example, even after the reading the documentation for xkey:

c   d       a b       c  
-------------------------
1 2 more    1 mini    1 2
3 4 complex 2 example 3 4
5 6 table   3 table   5 6

The solution provided is show bigExample: 0!complexTab!example which results in what I believe is required:

c   d       a b       c           
----------------------------------
1 2 more    1 mini    3.14        
3 4 complex 2 example 2.72        
5 6 table   3 table   2.997925e+08

It seems that xkey cannot be used to unkey a table here. I am uncertain as to why this is the case.

(It may be worth noting that example only had columns a and b initially. Column c was later created via

example[`c]: 3.14 2.72 299792458)

 

This issue does not occur for another question (in the Tables Exercises notebook) which involves unkeying a table. 

The question is as follows:

tradeKey is:

id  | sym price size side
----| -------------------
1001| IBM 79    117  B   
1002| GE  88    123  S   
1003| JPM 83    112  B   
1004| BP  74    166  S   
1005| JPM 87    136  B   
1006| BP  54    137  S   
1007| JPM 95    144  B   
1008| ASD 91    128  B   
1009| JPM 52    120  B   
1010| IBM 89    130  B   

The question asks us to unkey the tradeKey table and assign it to the variable unkeyedTrade.

The solution provided is:

unkeyedTrade:0!tradeKey
//or
unkeyedTrade:() xkey tradeKey
show unkeyedTrade

Either one results in:

id   sym price size side
------------------------
1001 IBM 79    117  B   
1002 GE  88    123  S   
1003 JPM 83    112  B   
1004 BP  74    166  S   
1005 JPM 87    136  B   
1006 BP  54    137  S   
1007 JPM 95    144  B   
1008 ASD 91    128  B   
1009 JPM 52    120  B   
1010 IBM 89    130  B   

(It may be worth noting that there was one modification made to tradeKey as part of an earlier question which resulted in what is seen now. The side for tradeKey id 1009 was modified to `B via

tradeKey[([]id:enlist 1009);`side]:enlist `B

That side used to be `S.)

 

I am uncertain as to why it seems that xkey cannot be used to unkey a table in the first question but it can be utilized in the second question.

 

I would greatly appreciate any assistance or clarification with this issue.

 

Thank you very much.

1 ACCEPTED SOLUTION

gyorokpeter-kx
Contributor
Contributor

The phenomenon you are seeing is because there are two columns named c in complexTab!example. In general duplicated column names in a table are a bad idea, and some functions even check for this and throw an error. But xkey just uses the # operator, which takes the first occurrence of the column to determine its value, so the first c column gets duplicated. ! is a built-in operator so it can use a different implementation to rearrange some pointers in memory that doesn't rely on the column names.

 

View solution in original post

2 REPLIES 2

gyorokpeter-kx
Contributor
Contributor

The phenomenon you are seeing is because there are two columns named c in complexTab!example. In general duplicated column names in a table are a bad idea, and some functions even check for this and throw an error. But xkey just uses the # operator, which takes the first occurrence of the column to determine its value, so the first c column gets duplicated. ! is a built-in operator so it can use a different implementation to rearrange some pointers in memory that doesn't rely on the column names.

 

MN_12
New Contributor III

Hi,

 

Thanks for the clarification! It has helped me get it now.