29 lines
973 B
Plaintext
29 lines
973 B
Plaintext
\\ Tokenize a string str according to 1 character delimiter d. Return a list of tokens.
|
|
\\ Using ssubstr() from http://rosettacode.org/wiki/Substring#PARI.2FGP
|
|
\\ stok() 3/5/16 aev
|
|
stok(str,d)={
|
|
my(d1c=ssubstr(d,1,1),str=Str(str,d1c),vt=Vecsmall(str),d1=sasc(d1c),
|
|
Lr=List(),sn=#str,v1,p1=1,vo=32);
|
|
if(sn==1, return(List(""))); if(vt[sn-1]==d1,sn--);
|
|
for(i=1,sn, v1=vt[i];
|
|
if(v1!=d1, vo=v1; next);
|
|
if(vo==d1||i==1, listput(Lr,""); p1=i+1; vo=v1; next);
|
|
if(i-p1>0, listput(Lr,ssubstr(str,p1,i-p1)); p1=i+1);
|
|
vo=v1;
|
|
);
|
|
return(Lr);
|
|
}
|
|
|
|
{
|
|
\\ TEST
|
|
print(" *** Testing stok from Version #2:");
|
|
\\ pp - positional parameter(s)
|
|
print("1. 5 pp: ", stok("Hello,How,Are,You,Today",","));
|
|
print("2. 5 pp: ", stok("Hello,How,Are,You,Today,",","));
|
|
print("3. 9 pp: ", stok(",,Hello,,,How,Are,You,Today",","));
|
|
print("4. 6 pp: ", stok(",,,,,,",","));
|
|
print("5. 1 pp: ", stok(",",","));
|
|
print("6. 1 pp: ", stok("Hello-o-o??",","));
|
|
print("7. 0 pp: ", stok("",","));
|
|
}
|