Manipulating list items, Sorting lists, Searching lists – Crunch CRiSP File Editor 6 User Manual

Page 24: Informational lists

Advertising
background image

Page 24

The make_list primitive is similar to quote_list() but each argument is evaluated in turn. For example:

list qlst. mlst;

qlst = quote_list(1, qlst);
mlst = make_list(1, qlst);

In this example, the list qlst will contain two elements -- the number '1', and the string 'qlst'. Remember
that none of the arguments are evaluated. Care needs to be taken with this primitive to avoid confusion. If
the value '1' had been replace by the expression, '1+2', then the resultant qlst would still have a length of
two with the first element have a value of 3. If, however, the expression '1' above were to be replaced with a
nonsensical expression such as '1+qlst', then the result qlst would still be a list of length 2, but the first
element would be a sub-list representing the expression 'one plus qlst'.

Now consider the make_list() example. In this case, the assignment to mlst would be a list of length three,
because each argument would be evaluated in turn. In this case the expression Bqlst is a list of length two
as defined in the previous statement.

You should try and understand these two very useful and powerful primitives because in many cases in the
CRiSP macros, lists are passed around, either to represent static strings in a menu (in which case
quote_list() is called) or as an easy means to pass variable length arguments to a function in an extensible
manner (in which make_list() is normally used).

{button See Also, ALink(crunch,,,)}

Manipulating List Items

CRiSP provides a fairly rich set of primitives for manipulating list items. Individual elements in a list can be
accessed either with the function nth() or by using square brackets, which are normally easier to read.
(The CRUNCH compiler converts the square bracket notation to a call to the function nth()).

list lst = {1, 2, 3};

message("lst[2]=%d", lst[2]);

Note that indices to elements in a list are zero based. The example above would print the message
"lst[2]=3".

Individual elements or sequences of elements can be deleted using the primitive delete_nth(). This primitive
takes two or three arguments, the first of which is the list to operate on, and the second is the index of the
first item to delete. If the third argument is present then this can be used to indicate how many consecutive
elements to delete. For example:

list lst = {1, 2, 3, 4};

lst = delete_nth(lst, 2);
/* lst == {1, 2, 4} */

{button See Also, ALink(crunch,,,)}

Sorting Lists

A list of strings can be sorted into alphabetical order using the sort_list() primitive. The order of the sort can
be controlled for increasing or decreasing alphabetical order.

{button See Also, ALink(crunch,,,)}

Searching Lists

Lists can be searched for strings and regular expression, using the re_search() primitive. Any non-string
elements in the list will be ignored.

{button See Also, ALink(crunch,,,)}

Informational Lists

CRiSP contains various primitives which return lists as their return value. These primitives allow the macro
programmers to enquire about the state of various objects within CRiSP.

Advertising