Guys,
I use my findpos() when I have the kind of problem I think Bill Ashton
is trying to solve. It's wordpos() on steroids. Doing NL I often have
to look for sets of chars or words in strings, but only want the
position of the first match. Neither pos, wordpos, nor parse deliver
that much precision.
/*
example:
The following example finds the position of the first occurance
of ; . ? or ! in a variable filled with english sentences.
endposofsentence = findpos('/; /. /? /! ',englishsentences' ',1,'/')
Bill's example:
files = '
pos.pro findpos.rex data.txt iexplore.exe'
endposoffilename = findpos('com txt htm pro exe rex',files' ',1,' ',1)
*/
/**------------------------------------------------------------------**/
/* FINDPOS SEARCHES FOR MULTIPLE CHARS IN A STRING, NOT JUST ONE. */
/**------------------------------------------------------------------**/
FINDPOS: PROCEDURE
PARSE ARG SCH,STR,POS,DLM,UPR,PFX,OPT /* OPT=1 MEANS RETURN FND VALUE*/
LOC = 0 /* INIT AT NOT FOUND */
if upr \= 1 then upr = 0
if upr then do
str = translate(str)
sch = translate(sch)
end
IF POS = 0 | DATATYPE(POS) \= 'NUM' THEN POS = 1
IF LENGTH(SCH) = 0 | LENGTH(STR) = 0 | POS > LENGTH(STR)
THEN NOP
ELSE DO UNTIL SCH == ''
IF DLM == ''
THEN PARSE VAR SCH 1 FND 2 SCH
ELSE IF DLM = ''
THEN PARSE VAR SCH FND SCH
ELSE PARSE VAR SCH FND (DLM) SCH
IF FND == '' THEN ITERATE
IF PFX = ''
THEN WRK = POS(FND,STR,POS)
ELSE WRK = POS(PFX||FND' ',STR,POS)
IF WRK = 0 THEN ITERATE
/* ONLY KEEP EARLIEST MATCH */
IF LOC = 0
THEN LOC = WRK FND
ELSE IF WORD(LOC,1) > WRK THEN LOC = WRK FND
END
IF OPT = 1
THEN RETURN LOC
ELSE RETURN WORD(LOC,1)
On 3/23/2012 5:33 PM, Bill Ashton wrote: