Shop OBEX P1 Docs P2 Docs Learn Events
PropBasic: INSTR function — Parallax Forums

PropBasic: INSTR function

VonSzarvasVonSzarvas Posts: 3,509
edited 2012-02-28 10:55 in Propeller 1
I needed an INSTR function for PropBasic... did not find one so created this code. Might not be the most efficient... anyhow I share it here:
INSTR           FUNC    2

'{$IFUSED INSTR}

FUNC INSTR '<source>, <search> ' Return 1 if search found in source, else return 0!


  instrSrc       VAR     __param1
  instrFind      VAR     Long
  instrCharS     VAR     __param3
  instrCharF     VAR     __param4
  instrmatch     VAR     Long


  instrFind = __param2
  
  DO

    RDBYTE instrFind, instrCharF
    IF instrCharF = 0 THEN EXIT      ' Reached the end of the find string!


    RDBYTE instrSrc, instrCharS      ' Read next src char into temp var
    IF instrCharS = 0 THEN
        instrmatch = 0               ' Clear match flag- we hit end of src before matching find string
        EXIT                         ' Reached the end of the search string! No more to do!
    ENDIF
    
     
    
    INC instrSrc                                         ' Advance pointer ready to read next src char
    INC InstrFind                                         ' Advance pointer ready to read next find char
    
    ' Compare
    IF instrCharF = instrCharS THEN
      instrmatch = 1                                 ' Set match flag as src and find chars match


    ELSE
      ' Reset pointers and counters and keep trying in the do-loop!
      instrFind = __param2
      instrmatch = 0
    ENDIF
  
  LOOP
  
  __param1 = instrmatch                             ' Return the result (1 or 0)
      
ENDFUNC
'{$ENDIF}

I use it something like this:
ping1name        DATA    "John Wayne",0


INSTR ping1name, "John"

IF __param1 = 1 THEN
   SEROUT TXPIN, TXBAUD, "Hello Mr Wayne!"
ELSE
   SEROUT TXPIN, TXBAUD, "Who are you?"
ENDIF





Reference some other string functions (ASSIGN, CONCAT, LEFT, MID, RIGHT) posted by Bean at post#410 here: http://forums.parallax.com/showthread.php?118611-Download-PropBASIC-here...-00.01.14-July-27-2011&p=903013&viewfull=1#post903013
Sign In or Register to comment.