# LOOKDOWN Examples

Syntax: LOOKDOWN

    ' LOOKDOWN.BS1
    ' This program uses LOOKDOWN followed by LOOKUP to map the numbers:
    ' 0, 10, 50, 64, 71 and 98 to 35, 40, 58, 62, 79, and 83, respectively.
    ' All other numbers are mapped to 255.

    ' {$STAMP BS1}
    ' {$PBASIC 1.0}

    SYMBOL  num     = W0                    ' holds current number
    SYMBOL  result  = W1                    ' holds mapped result

    Main:
      FOR num = 0 TO 100
        result = 255                        ' default value for no match
        LOOKDOWN num, (0, 10, 50, 64, 71, 98), result
        LOOKUP result, (35, 40, 58, 62, 79, 83), result
        DEBUG "Num = ", #num, "Result = ", #result, CR
        PAUSE 100
      NEXT
      END

NOTE: The example below is written for the BS2 and will run on any of the BS2-family modules. Modify the $STAMP directive (as required) before downloading to the BS2e, BS2sx, BS2p, BS2pe, or BS2px.

    ' LOOKDOWN.BS2
    ' This program uses LOOKDOWN to determine the number of decimal digits in
    ' a number. Since LOOKDOWN uses a zero-indexed table, the output will be
    ' the number of digits minus one, so this gets corrected in the following
    ' line.  Note that zero is considered a valid number and has one digit.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    aNum            VAR     Word            ' random number
    stpSz           VAR     Word            ' FOR-NEXT step size
    numDig          VAR     Nib             ' digits in aNum

    Setup:
      stpSz = 2

    Main:
      FOR aNum = 0 TO 15000 STEP stpSz
        LOOKDOWN aNum, <[0, 10, 100, 1000, 10000, 65535], numDig
        ' right-justify output
        DEBUG "aNum = ", REP " "\(5-numDig), DEC aNum, TAB,
              "Digits = ", DEC numDig, CR
        PAUSE 250
        LOOKDOWN aNum, <[0, 10, 100, 1000, 10000, 65535], stpSz
        LOOKUP stpSz, [2, 2, 5, 25, 250, 500, 1000], stpSz
      NEXT
      END

Go to Welcome page

BASIC Stamp Help Version 2.5.4

Copyright © Parallax Inc.

8/8/2012