BASIC stamp manual v2.2

LOOKDOWN – BASIC Stamp Command Reference
Page 276 BASIC Stamp Syntax and Reference Manual 2.2 www.parallax.com
' 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
Demo Program (LOOKDOWN.bs2)
' 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 ' the number to study
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
All
2
NOTE: This example program can be
used with all BS2 models by changing
the $STAMP directive accordingly.