Shop OBEX P1 Docs P2 Docs Learn Events
pBasic questions - Strings / Characters — Parallax Forums

pBasic questions - Strings / Characters

stampedstamped Posts: 68
edited 2006-10-20 01:54 in Learn with BlocklyProp
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

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2006-10-19 14:14
    1) You can use the STR formatting directive. Look in the PBasic manual description of SERIN for examples and details

    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
    nameTbl DATA 3,"abc", 3,"xyz", 3,"acc",0
    
    


    You could then search like this
    lookup:
       namePtr = nameTbl
       nameInd = 0
    loop:
       READ namePtr,size
       IF size = 0 THEN quit
       FOR i = 1 to size
         READ namePtr+i,char
         IF RxString[noparse][[/noparse]i-1] <> char THEN mismatch
       NEXT
       IF RxString[noparse][[/noparse]size] <> 0 THEN mismatch
       RETURN
    mismatch:
       nameInd = nameInd + 1
       namePtr = namePtr + size + 1
       goto loop
    quit:
       nameInd = -1
       return
    
    


    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.
  • stampedstamped Posts: 68
    edited 2006-10-20 01:54
    Thanks Mike. I thought the character comparison would be byte by byte. This is ok, just would have been nice to have some convenience String handling.

    I will check out the STR directive for SERIN. Thanks again.
Sign In or Register to comment.