Skip to content

sublist

Select a sublist of a list

x sublist y    sublist[x;y]

Where

  • x is an integer atom or pair
  • y is a list

returns a sublist of y. The result contains no more items than are available in y.

Head or tail

Where x is an integer atom, returns up to x items from the beginning of y if x is positive, or from the end if x is negative.

q)p:2 3 5 7 11
q)3 sublist p                           / 3 from the front
2 3 5
q)10 sublist p                          / only available values
2 3 5 7 11
q)2 sublist `a`b`c!(1 2 3;"xyz";2 3 5)  / 2 keys from a dictionary
a| 1 2 3
b| x y z
q)t:([]s:3 0 3 3 0;p:1 2 1 3 4;qty:100 200 200 300 400)
q)-3 sublist t                          / last 3 rows of a table
s p qty
-------
3 1 200
3 3 300
0 4 400

Taking a sample from the beginning of a string with Take returns unexpected results if the string is shorter than the sample, because Take repeats items to fill the count.

q)10#"take me"
"take metak"

Instead, compose Pad with sublist.

q){x$x sublist y}[10;]"take me"
"take me   "

Slice

Where x is an integer pair, returns up to x[1] items from y, starting at item x[0].

q)1 2 sublist p  / 2 items starting from position 1
3 5
q)3 10 sublist p / only available values
7 11

Take

Selection