LOOKUP, LOOKUPZ

Command: Get value from an indexed position within a list.

((PUBPRI))
   LOOKUP ( Index : ExpressionList )


((PUBPRI))
   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, 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.

If LOOKUPZ is used, the list is zero-based (0..N-1) instead of one-based; an Index of 0 returns 25, Index of 1 returns 300, etc.

If Index is out of range 0 is returned. So, for LOOKUP, if the REPEAT statement went from 0 to 8, instead of 1 to 7, this example would print 0, 25, 300, 2510, 163, 17, 8000, 3 and 0 on a display.

Unless otherwise noted, content on this site is licensed under the
Creative Commons Attribution-ShareAlike 4.0 International License.