Shop OBEX P1 Docs P2 Docs Learn Events
Comparing Strings — Parallax Forums

Comparing Strings

KevinWiebeKevinWiebe Posts: 4
edited 2008-11-17 16:41 in BASIC Stamp
I am looking for an easy way to compare strings in a BS2. The string was read into an array with this command, SERIN LCDin,Baud,[noparse][[/noparse]STR serStr\9]. Ideally I would to use something as simple as, IF serStr <> "bla bla bla" THEN...

Any help would be great!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-11-16 20:01
    You have to compare strings one character at a time and you can't compare a string directly against a character string in your program as you've written as an example of what you'd like.

    The easiest thing to do would be to use DATA statements to place constant strings into EEPROM and write a subroutine to compare your variable against a string given its address in EEPROM. You could also write a subroutine to look up a string in a table in EEPROM like:
    Bla1  data "BlaBlaBla",0
          data "MoreBla",0
    
    i   var word
    j   var word
    x  var byte
    z   var byte
    ' This is called by setting i to the address of the EEPROM string to compare
    ' It returns with z zero if the match was successful or z non-zero if a mismatch
    ' If there's a mismatch, i is left pointing to the next string in EEPROM
    compare:
       j = -1  ' Start j one position before start of string
    check:
       read i,x : i = i + 1 : j = j + 1  ' Get a character and increment pointers
       if serStr(j) = x then goto check  ' Still matching, continue comparing
       z = x
    findEnd:
       if x = 0 then return  ' Make sure i points to the next string
       read i,x : i = i + 1
       goto findEnd
    

    Post Edited (Mike Green) : 11/16/2008 8:07:51 PM GMT
  • KevinWiebeKevinWiebe Posts: 4
    edited 2008-11-17 16:34
    Thanks Mike! I was hoping for an easier solution but I looks like this is as good as it gets.

    I am new to STAMPs and am using a BS2 for development. It has quickly become obvious I need to move up to the larger STAMPs.

    Thanks again.
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-11-17 16:41
    ALL BS2 'flavor's have only 26 bytes of RAM. The 'larger stamps' have more eeprom space (for multi-slot programming) and run faster, but they STILL only have 26 bytes of RAM.

    If you're used to more traditional processors, the 28SX and 48SX chips (and the boards, I love the boards) will run at 50 MIPS and support a real hardware interrupt, you might check those out.
Sign In or Register to comment.