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

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

;Returns 0-based index of token.

;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

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

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

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

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

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

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.
Thank you both for your kind help.

Forum Jump:

Users browsing this thread: 1 Guest(s)