<< Click to Display Table of Contents >>

List functions:

 

Note that some of the functions assume that the lists are lists of strings.

 

Function name

Description

Examples

[x]-index

Index: picks out a single element (at integer x position). In contrast to R, this does not return a 1-element list containing the variable. If you need that, use for instance #m[3..3].

Returns: var

#m[3];  //the third element

[x1..x2]-index

Index: picks out a range of elements. You may omit x1 or x2.

Returns: list

#m[3..5];  //the third to fifth elements

#m[3..];

[x1, x2]-index

For a nested list of lists, #m[3, 5] will return the same element as #m[3][5], so this is just a convenience to make a nested list accessible like a matrix. See more here.

Returns: variable

#= ((1, 2), (3, 4));
prt #m[2, 1], #m[2][1];  //same

[x1..y1, x2..y2]-index

[x1..y1, x2]-index

[x1, x2..y2]-index

For a nested list of lists, #m[2..3, 2..4] will select the given 'rows" and "columns", corresponding to selecting a submatrix from a matrix. Beware that in general, #m[2..3, 2..4] is completely different from #m[2..3][2..4].  See more here.

Returns: list

//  1  2  3
//  4  5  6
//  7  8  9
// 10 11 12
#= ((1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12));
prt #m[2, 2..3];
prt #m[2][2..3]; //same as above
prt #m[2..4, 2]; //matrix-like selection
prt #m[2..4][2]; //different from above!
prt #m[2..4, 2..3]; //matrix-like selection
prt #m[2..4][2..3]; //different from above!

append(x1, x2)

append(x1, i, x2)

Adds variable x2 as it is at the end of list x1. Note that if x2 is a list of for instance 3 items, only 1 element is added (the list itself). If you need to add the 3 elements individually, use extend().

 

If used with i argument, x2 is inserted at index i, instead of at the end. See also extend().

 

To prepend, use append(x1, 1, x2).

 

Returns: list

#= #x1.append(#x2);  //or: append(#x1, #x2)
#= #x1.append(2, #x2);  //insert at position 2

contains(x1, x2)

Checks if the list of strings x1 contains the string x2. Returns 1 if true, 0 otherwise. You may alternatively use "x2 in x1", see the last example. See also the count() and index() functions. The comparisons are case-insensitive.

Returns: val

%= #x1.contains(%s);
if(#x1.contains(%s) == 1); tell 'yes'; end;
if(%s in #x1); tell 'yes'; end;

count(x1, x2)

Counts the number of times the string x2 is present in the list of strings x1. See also the contains() and index() functions.

 

Note: to obtain the number of elements in a list, use the length() function. The comparisons are case-insensitive.

 

Returns: val

%= #x1.count(%s);  //or: count(#x1, %s)

data(x)

Accepts a string of blank-separated values x and turns them into a list of values. This is handy for long sequences of blank-separated numbers, instead of manually setting the commas.

Returns: list

#= data('1.0  2.0  1.5');

dates(x)

Tries to convert each element of the list x to a date.

Returns: list

#= dates(#x);

except(x1, x2)

 

clip0051

The except() function subtracts x2 from x1. You may alternatively use the operator -. Only works for lists of strings. See also intersect() and union(). See also extend().

 

Returns: list

#= #x1.except(#x2);  //or: except(#x1, #x2)
#= #x1 - #x2;        //same
 
#-= #x1;  //subtract from itself

extend(x1, x2)

extend(x1, i, x2)

The arguments x1 and x2 must be lists. The function inserts the elements of list x2 one by one at the end of (or at position i in) the list x1. The resulting list may contain dublets.

 

For two lists x1 and x2, you may alternatively use the + operator. See also except() and append().

 

To pre-extend, use extend(x1, 1, x2).

 

Returns: list

#= #x1.extend(#x2);  //or: extend(#x1, #x2)
#= #x1 + #x2; //same as above
#= #x1.extend(2, #x2);  //insert at position 2
 
#+= #x1; //add to itself

flatten(x)

For at list x, the function returns a flattened version of the list. For instance, the list (1, (2, 3)) is transformed into a non-recursive list of non-list elements: (1, 2, 3).

 

Returns: list

 

#m1 = (1, (2, 3));
#m2 = #m1.flatten(); //or: flatten(#m1).

index(x1, x2)

Returns the index of the first occurrence of the string x2 in the list of strings x1. Returns 0 if x2 is not found in x1. See also the count() and contains() functions. The comparisons are case-insensitive.

Returns: value (integer)

%= #x1.index(%s);  //or: index(#x1, %s)

intersect(x1, x2)

 

clip0050

The intersect() function finds the common elements of the two list of strings x1 and x2. The resulting list will not contain dublets. You may alternatively use the operator &&. Only works for lists of strings. See also except() and union().

Returns: list

#= #x1.intersect(#x2);  //or: intersect(#x1, #x2)
#= #x1 && #x2;

join(x1, x2)

For a string x1 and a list of strings x2, the method concatenates the x2 elements with x1 as separator. See also path().

Returns: string

#= ('x1', 'x2', 'x3');
%= join(', ', #m); //'x1, x2, x3'

length(x)

len(x)

Returns the number of elements in the list x. You may use len() instead of length().

Returns: val

%= #x.length();  //or: length(#x).
%= #x.len(); //the same

list(x1, x2, ...)

Returns a list of the variables x1, x2, etc. The function is handy for lists with only 0 or 1 elements. See examples.

Returns: list

#= ();      //will fail
#= list();  //ok: empty list

#= (1, 2);  //easy
#= (1);     //will fail
#= (1,);    //is ok
#= list(1); //is ok

lower(x)

Returns string elements in the list x as lower-case.

Returns: list

#= #x1.lower();  //or: lower(#x1)

path(x)

For a list of strings x, the method concatenates the x elements with '\' as separator. See also join(), pathparts().

Returns: string

#= ('x1', 'x2', 'x3');
%= path(#m); //'x1\x2\x3'

pop(x1, i)

pop(x1)

Removes the element at position i in the list x1. Removes the last element if called with pop(x).

Returns: list

#= #x1.pop(2);  //or: pop(#x1, 2)
#= #x1.pop();  //last element
#= #x1.pop(1);  //first element

preextend(x1, x2)

Same as extend(x1, 1, x2), putting the elements of x2 in the first position of x1.

#= #x1.preextend(#x2);  //insert at position 1

prefix(x1, x2)

If x1 is a list of strings, each element has the string x2 prefixed (prepended)

Returns: list

#= #x1.prefix(%s);  //or: prefix(#x1, %s);

prepend(x1, x2)

Same as append(x1, 1, x2), putting x2 in the first position of x1.

#= #x1.prepend(#x2);  //insert at position 1

sort(x)

sort(x, 'natural')

Returns a sorted list of strings, provided that x is a list of strings. Sorting is case-insensitive.

When the 'natural' argument is used, strings containing numbers will sort naturally, for instance returning a8, a9, a10, instead of a10, a8, a9.

Returns: list

#= #x.sort();  //or: sort(#x)
#= #x.sort('natural');

remove(x1, x2)

Removes any string x2 from the list of strings x1. See also the except() function.

Returns: list

#= #x1.remove(%s);  //or: remove(#x1, %s);

#= #x1 - %s;        //also legal

reverse(x)

Reverses list items.

Returns: list.

#= a, b, c;
prt #x; //a, b, c
prt #x.reverse(); //c, b, a

replace(x1, x2, x3)

replaceinside(x1, x2, x3)

replaceinside(x1, x2, x3, max)

replace(): In the list of strings x1, if this string element is the same as x2, x3 is inserted instead.

 

replaceinside(): the string element has any occurences of x2 inside the string replaced with x3. The replacements may be limited via the max argument.

 

Returns: list

#= #x1.replace(%x2, %x3); //or: replace(#x1, %x2, %x3)
 
#= #x1.replaceinside(%x2, %x3);

strings(x)

Tries to convert each element of the list x to a string

Returns: list

#= strings(#x);

suffix(x1, x2)

If x1 is a list of strings, each element has the string x2 suffixed (appended)

Returns: list

#= #x1.suffix(%s);  //or: suffix(#x1, %s);

t(x)

For a nested list of lists x, the t() function returns the transpose, similar to transposing a matrix.

Returns: list (of lists)

#= ((1, 2), (3, 4));
#m, t(#m);

union(x1, x2)

 

clip0049

The union() function finds the union of the two lists x1 and x2. Alternatively use the operator ||. The resulting list will not introduce dublets, in contrast to the similar + operator (simple concatenation). Only works for lists of strings. See also except() and intersect().

Returns: list

#= #x1.union(#x2);  //or: union(#x1, #x2)
#= #x1 || #x2;

unique(x)

Retains only those elements of list x that are unique (list of strings only).

Returns: list

#= #x1.unique();  //or: unique(#x1)

upper(x)

Returns string elements in the list x as upper-case.

Returns: list

#= #x1.upper();  //or: upper(#x1)

vals(x)

Tries to convert each element of the list x to a value

Returns: list

#= vals(#x);

venn(x1, x2)

 

 

For two lists of strings, this function prints out lists corresponding to a Venn diagram.

 

clip0150

 

That is, intersection and two differences. For instance, venn(#m1, #m2) corresponds to printing out the following:

 

#m1 && #m2

#m1 - #m2

#m2 - #m1

 

See also intercept(), except() and union().

Returns: nothing.

#m1 = a, b, c, d, e;
#m2 = c, d, e, f, g;
venn(#m1, #m2);
 

//Results:

 
//The following 3 strings are in both lists:
//'c', 'd', 'e'
 
//The following 2 strings are in list #1, but not in list #2:
//'a', 'b'
 
//The following 2 strings are in list #2, but not in list #1:
//'f', 'g'