Shop OBEX P1 Docs P2 Docs Learn Events
Case with String — Parallax Forums

Case with String

I try to get my stuff compact.

In try to change

elseif STR.startsWithCharacters(STR.tokenizeCharacters(@filename[0]), String("INITIAL")) == TRUE
     do x

elseif STR.startsWithCharacters(STR.tokenizeCharacters(@filename[0]), String("LEFT")) == TRUE
     do y
......

It works try to cleanup with "case" statement

Like

case    STR.tokenizeCharacters(@filename[0])

                              string("INITIAL") :  do x

                              string("LEFT")    :   do y
And it will not do it

Somebody an direction ?

Thanks

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2015-10-14 17:14
    You'll likely need to do a string compare algorithm.

    The line "string("INITIAL")" produces an address where "INITIAL" is stored in RAM. Each string statement will produce a new address so you can't compare this way.

    Here's one way I've used. This technique will only compare the first four characters so you need to make sure the first four characters of each string are unique.
    PUB ExecuteCmd(stringPtr)
    
      bytemove(@result, stringPtr, 4)
    
      case result
        initTxt: 
          ' do stuff to initialize
        leftTxt:
          ' do left stuff
    
    DAT
    initTxt       long
                  byte "INITIAL", 0
    leftTxt       long
                  byte "LEFT" , 0
    

    By defining the strings as longs before listing the characters in the string, the first four characters of the string can be treated as an independent long value.

    Again, the limit of doing it this way is only the first four bytes of the string are tested. If a string has fewer than four characters, it should be padded with zeros at the end so it makes up a full long (though I'm not positive this is needed).

    The above is an unconventional way to testing strings. For a more conventional way you can see how it's done in the Eddie firmware.

    https://github.com/ddegn/EddieFirmware

    The file "Eddie.spin" should show how this is done. If you want a complete program, download one of the archives.

    Here's a section showing how "strcmp" is used.
      if strcomp(@InputBuffer, string("ACC"))
        ' do ACC stuff
      elseif strcomp(@InputBuffer, string("ADC"))         
        ' do ADC stuff
    

    The string being compared needs to be the correct case and stored in the buffer named "InputBuffer."
Sign In or Register to comment.