Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Returns 0-based index of token
#1
QM fellows, do you have any better idea?

Function findtx
Code:
Copy      Help
function# str's str'target

;Returns 0-based index of token.

;Example
;;
;str s="jan feb mar apr"
;
;str target="mar"
;
;out findtx(s target) ;;2

for int'i 0 100
,str s1.gett(s)
,if(s1=target) ret i;; 0-based index of token
,if(empty(s1)) ret -1;; Not found
#2
Hello

str s="jan feb mar apr"
str target="feb"
_i=findtx(s target)
out _i


function findtx
function# str'&s str'&target

_i=0
ARRAY(str) test
int nb=tok(s test)

for _i 0 nb
if(!strcmp(test[_i] target)) ret _i


HTH
#3
if(empty(s1)) ret -1
can be removed because gett returns -1 when there are no more tokens

Code:
Copy      Help
function# $s $target

for int'i 0 1000000000
,if(_s.gett(s -i)<0) ret -1;; Not found
,if(_s=target) ret i;; 0-based index of token

With ARRAY also OK.
#4
Thank you both for your kind help.


Forum Jump:


Users browsing this thread: 1 Guest(s)