pBasic questions - Strings / Characters
stamped
Posts: 68
I have two general questions regarding pBasic:
1) How do I read data direct into an array using the SERIN command without having to specify the element indexes of the byte array?
Eg:
RxString VAR Byte(3)
' Want to avoid having to give the addresses in the array
SERIN RxLine, 16468, [noparse][[/noparse]WAIT("!"), RxString(0), RxString(1), RxString(2)]
2) I am pondering how to compare two strings in pBasic
I have the following:
RxString VAR Byte(3)
RxString(0) = "a"
RxString(1) = "b"
RxString(2) = "c"
and I want to use a LOOKDOWN or similar to locate which index which the string equals.
Eg:
ind VAR NIB
LOOKDOWN (RxString(0) + RxString(1) + RxString(2)), [noparse][[/noparse]"xyz", "abc", "acc"], ind
I cannot work out how to do the above LOOKDOWN idea. It appears that I cannot add two characters as it is adding their ASCII values and not their chars, so the ind (index) is wrong. Do I need to wrap the character additions in DIG or something to concat the chars?
Any pointers are much appreciated.
Post Edited (stamped) : 10/19/2006 1:45:46 PM GMT
1) How do I read data direct into an array using the SERIN command without having to specify the element indexes of the byte array?
Eg:
RxString VAR Byte(3)
' Want to avoid having to give the addresses in the array
SERIN RxLine, 16468, [noparse][[/noparse]WAIT("!"), RxString(0), RxString(1), RxString(2)]
2) I am pondering how to compare two strings in pBasic
I have the following:
RxString VAR Byte(3)
RxString(0) = "a"
RxString(1) = "b"
RxString(2) = "c"
and I want to use a LOOKDOWN or similar to locate which index which the string equals.
Eg:
ind VAR NIB
LOOKDOWN (RxString(0) + RxString(1) + RxString(2)), [noparse][[/noparse]"xyz", "abc", "acc"], ind
I cannot work out how to do the above LOOKDOWN idea. It appears that I cannot add two characters as it is adding their ASCII values and not their chars, so the ind (index) is wrong. Do I need to wrap the character additions in DIG or something to concat the chars?
Any pointers are much appreciated.
Post Edited (stamped) : 10/19/2006 1:45:46 PM GMT
Comments
2) Unfortunately, you have to do the string comparisons yourself a byte at a time. There are no built-in operators that operate on strings and characters are just like any other byte value.
The Stamps have very very limited memory for data (26 bytes) and it just doesn't make sense to have built-in string operators. It's pretty easy to write a subroutine that does the kind of search you want. You can set up a table in EEPROM using the DATA statement, prefix each string with its length with a zero byte at the end of the table like
You could then search like this
This subroutine sets nameInd to -1 if there's no match or to the 0 based index if there is a match. The string in RxString needs to be terminated with a zero byte for this routine to work.
I will check out the STR directive for SERIN. Thanks again.