Propeller Manual

Table Of Contents
LOOKUP, LOOKUPZ – Spin Language Reference
Page 138 · Propeller Manual v1.1
LOOKUP, LOOKUPZ
Command: Get value from an indexed position within a list.
((PUB PRI))
LOOKUP ( Index : ExpressionList )
((PUB PRI))
LOOKUPZ ( Index : ExpressionList )
Returns: Value at the one-based Index position (LOOKUP) or zero-based Index position
(
LOOKUPZ) of ExpressionList, or 0 if out-of-range.
Index is an expression indicating the position of the desired value in ExpressionList.
For
LOOKUP, Index is one-based (1..N). For LOOKUPZ, Index is zero-based (0..N-1).
ExpressionList is a comma-separated list of expressions. Quoted strings of characters
are also allowed; they are treated as a comma-separated list of characters.
Explanation
LOOKUP and LOOKUPZ are commands that retrieve entries from a list of values. LOOKUP returns
the value from ExpressionList that is located in the one-based position (1..N) given by Index.
LOOKUPZ is just like LOOKUP except it uses a zero-based Index (0..N-1). For both commands, if
Index is out of range then 0 is returned.
Using LOOKUP or LOOKUPZ
LOOKUP and LOOKUPZ are useful for mapping a contiguous set of numbers (1, 2, 3, etc. –or– 0,
1, 2, etc.) to a set of non-contiguous numbers (45, -103, 18, etc.) where no algebraic
expression can be found to do so concisely. The following example assumes
Print is a
method created elsewhere.
PUB ShowList | Index, Temp
repeat Index from 1 to 7
Temp := lookup(Index: 25, 300, 2_510, 163, 17, 8_000, 3)
Print(Temp)
This example looks up all the values in LOOKUP’s ExpressionList and displays them. The
REPEAT loop counts with Index from 1 to 7. Each iteration of the loop, T LOOKUP uses Index to
retrieve a value from its list. If
Index equals 1, the value 25 is returned. If Index equals 2, the
value 300 is returned. Assuming
Print is a method that displays the value of Temp, this
example will print 25, 300, 2510, 163, 17, 8000 and 3 on a display.