Specifications

BASIC Stamp II
Parallax, Inc. • BASIC Stamp Programming Manual 1.8 • Page 279
2
Debug prints, “Value matches item 5 in list” because the value (17)
matches item 5 of [26,177,13,1,0,17,99]. Note that index numbers count
up from 0, not 1; that is in the list [26,177,13,1,0,17,99], 26 is item 0.
What happens if the value doesn’t match any of the items in the list?
Try changing “value = 17” to “value = 2.” Since 2 is not on the list,
Lookdown does nothing. Since result contained 15 before Lookdown
executed, Debug prints “Value matches item 15 in list.” Since there is
no item 15, the program should look upon this number as a no-match
indication.
Don’t forget that text phrases are just lists of byte values, so they too
are eligible for Lookdown searches, as in this example:
value var byte
result var byte
value = "f"
result = 255
LOOKDOWN value,["The quick brown fox"],result
debug "Value matches item ",dec result," in list"
Debug prints, “Value matches item 16 in list” because the phrase “The
quick brown fox” is a list of 19 bytes representing the ASCII values of
each letter. A common application for Lookdown in conjunction with
the Branch instruction, is to interpret single-letter instructions:
cmd var byte
cmd = "M"
LOOKDOWN cmd,["SLMH"],cmd
Branch cmd,[stop_,low_,medium,high_]
debug "Command not in list": stop
stop_: debug "stop": stop
low_: debug "low": stop
medium: debug "medium": stop
high_: debug "high": stop
In that example, the variable cmd contains “M” (ASCII 77). Lookdown
finds that this is item 2 of a list of one-character commands and stores 2
into cmd. Branch then goes to item 2 of its list, which is the program
label “medium” at which point the program continues. Debug prints
“medium” on the PC screen. This is a powerful method for interpreting
user input, and a lot neater than the alternative If...Then instructions.