Author Topic: Follow the development of QB64 v1.3  (Read 20245 times)

0 Members and 1 Guest are viewing this topic.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Follow the development of QB64
« Reply #45 on: April 02, 2019, 02:37:55 am »
I'm borrowing the name and the behaviour from VB, and it did make sense to me to specify a start position from left to right instead of the contrary.

Well give it back and borrow this, instead...

Code: QB64: [Select]
  1. sstrng$ = "*,@,h,&,h,a,b,c,d,e,f,g,h,1,2,h"
  2. srch$ = "h"
  3. seed& = 2
  4. PRINT instrrev&(seed&, strng$, srch$)
  5.  
  6. FUNCTION instrrev& (seed&, strng$, srch$)
  7. b$ = SPACE$(LEN(strng$))
  8. FOR i& = LEN(b$) TO 1 STEP -1
  9.     j& = j& + 1
  10.     MID$(b$, j&, 1) = MID$(strng$, i&, 1)
  11. instrrev& = LEN(b$) - INSTR(seed&, b$, srch$) + 1

So I'm asking it to get the first "h" that is 2 or more characters from the right. The example above returns 25, which tells me in my string, the "h" I want located if 25 characters from the left. That's useful for very long strings, where you know what you are looking for is repeated several times throughout the string, but the one you want is very near the end. Maybe this is what you are describing too, as I cannot see a way to seed it from left to right in reverse. I mean using 10 in the above example seems weird to me when I know I want the "h" two or more characters in from the right.

Pete
« Last Edit: April 02, 2019, 02:42:33 am by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/