Q Language - Lists


Advertisements

Lists are the basic building blocks of q language, so a thorough understanding of lists is very important. A list is simply an ordered collection of atoms (atomic elements) and other lists (group of one or more atoms).

Types of List

A general list encloses its items within matching parentheses and separates them with semicolons. For example −

(9;8;7)   or   ("a"; "b"; "c")   or   (-10.0; 3.1415e; `abcd; "r")

If a list comprises of atoms of same type, it is known as a uniform list. Else, it is known as a general list (mixed type).

Count

We can obtain the number of items in a list through its count.

q)l1:(-10.0;3.1415e;`abcd;"r")    / Assigning variable name to general list

q)count l1                        / Calculating number of items in the list l1
4

Examples of simple List

q)h:(1h;2h;255h)                    / Simple Integer List

q)h
1 2 255h

q)f:(123.4567;9876.543;98.7)        / Simple Floating Point List

q)f
123.4567 9876.543 98.7

q)b:(0b;1b;0b;1b;1b)                / Simple Binary Lists

q)b
01011b

q)symbols:(`Life;`Is;`Beautiful)    / Simple Symbols List

q)symbols
`Life`Is`Beautiful

q)chars:("h";"e";"l";"l";"o";" ";"w";"o";"r";"l";"d") 
                                    / Simple char lists and Strings.
q)chars
"hello world"

**Note − A simple list of char is called a string.

A list contains atoms or lists. To create a single item list, we use −

q)singleton:enlist 42

q)singleton
,42

To distinguish between an atom and the equivalent singleton, examine the sign of their type.

q)signum type 42
-1i

q)signum type enlist 42
1i
Advertisements