Must all Strings be define in array
DosEdge
Posts: 33
I was trying to declare·a string to a variable.· Then, I wanted to compare that variable to an input from the DEBUGIN command.· I assume that all strings must be define as an array.· You can not initialize a variable in PBasic as you would with any other high level programming language.· Can you use the DEFINE directive and initialize a variable?
Comments
You can't initialize a variable except by storing information in it using an assignment statement.
You can store constant strings in EEPROM that you can declare in your program. Look at the READ/WRITE/DATA statements in the manual. This is a common way to store strings used to interpret input information like from a DEBUGIN statement.
vName·Var· Byte(3)
vName = "yes"
or
#DEFINE vName = "yes"
You could also copy the characters from EEPROM with:
I don't recommend the above since it uses your very limited
variable space for what is essentially a string constant.
Better to have the string in EEPROM and compare it character
by character to the received DEBUGIN string like:
Note that it's an easy extension of this to have a table of responses and to search through it looking for a match. If there's a match, the index (number) of the matched response can be left in a variable for some use later.
#DEFINE Monitor = yes····· 'defines variable to a string
vMonitor VAR Byte(3)······· 'declares a variable for three characters
DEBUGIN STR vMonitor \ 3··'input three characters through terminal
IF vMonitor = Monitor· THEN 'compare strings
ReadCurrentTemp:
The only kind of variables in PBasic are 16-bit words, 8-bit bytes, 4-bit nibbles, and individual bits. A numeric value can be used to hold a character and an array of numeric values can be used to hold a short string (since there are only 26 bytes of variable space available), but no string manipulation is possible except on a byte by byte explicitly programmed basis. Sorry.
Thank You Mike,
DosEdge
#DEFINE Monitor = yes 'defines variable to a string
vMonitor VAR Byte(3) 'declares a variable for three characters
DEBUGIN STR vMonitor \ 3 'input three characters through terminal
IF vMonitor = Monitor THEN 'compare strings
ReadCurrentTemp:
"
Well, actually, there IS some "string" handling stuff -- mostly the 'STR' modifier.
' {$STAMP BS2}
' {$PBASIC 2.5}
TempByte VAR Byte
I VAR Byte
String1 DATA "YES" ' Stores the characters "Y", "E", "S" in EEPROM, storing the "Y" in String1(0).
vMonitor VAR Byte(4) ' Declares 3 byte 'byte array' as vMonitor(0), vMonitor(1), vMonitor(2)
MAIN:
· DEBUGIN STR vMonitor\3 ' Should read in a 3-character string into vMonitor 0..2, and put null in vMonitor(3).
· ' Not sure if it really does this though...
· FOR I = 0 TO 2
··· READ String1+I, TempByte
··· IF TempByte <> vMonitor(I) THEN
····· GOTO MisMatch
··· ENDIF
· NEXT
' Match! So:
GOSUB ReadTemp
MisMatch:
· GOTO MAIN
ReadTemp:
· RETURN
Post Edited (allanlane5) : 2/2/2007 9:45:35 PM GMT
Thank you Mike and allanlane5,
Dosedge