QforMortals3/Lists 101

From Kx Wiki
Jump to: navigation, search

1.9 Lists 101

The fundamental q data structure is a list, which is an ordered collection of items sequenced from left to right. The notation for a general list encloses items with ‘(’ and ‘)’ and uses ‘;’ as separator. Spaces after the semi-colons are optional but can improve readability.

q)(1; 1.2; `one)

A few observations on lists.

q)(1; 2; 3)
1 2 3
q)(1.2; 2.2; 3.3)
q)(2000.01.01; 2000.01.02; 2000.01.03)

A simple list of booleans is juxtaposed with no spaces and has a trailing `b’ type indicator.

q)(1b; 0b; 1b)

A simple list of symbols is displayed with no separating spaces.

(`one; `two; `three)

Homogenous lists of atoms can be entered in either general or simplified form. Regardless of how they are created, q recognizes a list of homogenous atoms dynamically and coverts it to a simple list.

Next we explore some basic operations to construct and manipulate lists. The most fundamental is (til), which takes a non-negative integer n and returns the first n integers starting at 0 (n itself is not included in the result).

q)til 10
0 1 2 3 4 5 6 7 8 9

We obtain the first 10 integers starting at 1 by adding 1 to the previous result. Be mindful that q always evaluates expressions from right to left and that operations work on vectors whenever possible.

q)1+til 10
1 2 3 4 5 6 7 8 9 10

Similarly, we obtain the first 10 even numbers and the first ten odd numbers.

q)2*til 10
q)1+2*til 10

Finally, we obtain the first 10 even numbers starting at 42.

q)42+2*til 10

Another frequently used list primitive is join (,) that returns the list obtained by concatenating its right operand to its left operand.

q)1 2 3,4 5
1 2 3 4 5
q)1 2 3,100
q)0,1 2 3

To extract items from the front or back of a list, use the take operator (#). Positive argument means take from the front, negative from the back.

q)2#til 10
0 1
q)-2#til 10
Information.png Tip: Applying (#) always results in a list.

In particular, the idiom 0# returns an empty list of the same type as the first item in its argument. Using an atom argument is a succinct way to create a typed empty list of the type of the atom.

q)0#1 2 3

Should you extract more items than there are in the list, (#) restarts at the beginning and continues extracting. It does this until the specified number of items is reached.

q)5#1 2 3
1 2 3 1 2

In particular, if you apply (#) to an atom, it will continue drawing that single atom until it has the specified number of copies. This is a succinct idiom to replicate an atom to a list of specified length.

42 42 42 42 42

As with atoms, a list can be assigned to a variable.

q)L:10 20 30

The items of a list can be accessed via indexing, which uses square brackets and is relative to 0.


Prev: Data Types 101, Next: Functions 101

Reprinted with the author's permission from: q for Mortals Version 3, An Introduction to Q Programming by Jeffry A. Borror.

The book is available on Amazon. In the United Kingdom, it is available at Amazon UK.

©2015 Jeffry A. Borror/ q4m LLC

Personal tools