My first working stock function. Pluribasic generates the call automatically:
? PARSE$("<>TWO<>TREE<>FOUR", "<>", 3)
? PARSE$("0<1>TWO<2>TREE<3>FOUR", any "<>", 3)
? PARSE$("0<1>TWO<2>TREE<3>FOUR", any "<>", 1)
Output:
TREE
TWO
0
If you call it directly using Oxygen code you can use it like this:
print PARSE("<>TWO<>TREE<>FOUR", 0, "<>", 3)
print PARSE("0<1>TWO<2>TREE<3>FOUR", 1, "<>", 3)
print PARSE("0<1>TWO<2>TREE<3>FOUR", 1, "<>", 1)
// returns a field of data given a separator.
FUNCTION PARSE(string src, long a, string sep, long fldnum) as string
if sep = "" then
return src
end if
indexbase 1
byte srcchar at strptr(src)
byte sepchar at strptr(sep)
long p1
long pos = 1
long curfld = 1
long index
long seps
for index = 1 to len(src)
if a then
for seps = 1 to len(sep)
if srcchar[index] = sepchar[seps] then
goto match
end if
next
if index = len(src) then
index += 1
else
goto nomatch
end if
elseif index = len(src) then
index += 1
else
for seps = 1 to len(sep)
if srcchar[index+seps-1] <> sepchar[seps] then
goto nomatch
end if
next
end if
match:
p1 = pos
pos = index
if fldnum = curfld then
return mid(src, p1, (pos-p1))
end if
curfld += 1
if a then
pos = index + 1
else
pos = index + len(sep)
end if
nomatch:
next
if fldnum = 1 then
return src
end if
END FUNCTION