Search code examples
kdb

KDB+/Q : How does the following code work ? q) 16 16#"c"$til 256`


How does the following code work in kdb+/q , specifically, what does the first 16 do ?

q)16 16#"c"$til 256

til 256 creates a list 0 .. 255, "c"$ casts each entry to type char, and 16#takes the first 16 elements, but what does the first 16 do ?

I cannot see this mentioned anywhere in the documentation, despite this being an example quoted here: https://code.kx.com/q4m3/4_Operators/#433-order

enter image description here


Solution

  • # operator will select leading or trailing items from a list or dictionary

    x#y     #[x;y]
    

    Where x is an int atom or vector, or a table; y is an atom, list, dictionary, table, or keyed table

    In your case x is a vector and returns a matrix or higher-dimensional array, for example

    q)2 4#`Arthur`Steve`Dennis
    Arthur Steve  Dennis Arthur
    Steve  Dennis Arthur Steve**
    

    By splitting up your example you can see the first and last lines returned from the array

    q)16#"c"$til 256
    "\000\001\002\003\004\005\006\007\010\t\n\013\014\r\016\017"
    q)-16#"c"$til 256
    "\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377"
    

    For more information https://code.kx.com/q/ref/take/