Tokenize a string: Difference between revisions

→‎{{header|Oz}}: adding 2 PARI scripts
mNo edit summary
(→‎{{header|Oz}}: adding 2 PARI scripts)
Line 1,426:
{System.printInfo T#"."}
end</lang>
 
=={{header|PARI/GP}}==
===Version #1.===
Simple version, like the most custom ones here (for this task). This version has 1 character delimiter,
which is not allowed in the beginning and at the end of string, in addition, double, triple, etc., delimiters
are not allowed too.
 
{{Works with|PARI/GP|2.7.4 and above}}
 
<lang parigp>
\\Tokenize a string str according to a 1 character delimiter d. Return a list of tokens.
\\ tokenize() 3/5/16 aev
tokenize(str,d)={
my(str=Str(str,d),vt=Vecsmall(str),d1=sasc(d),Lr=List(),sn=#str,v1,p1=1);
for(i=p1,sn, v1=vt[i]; if(v1==d1, listput(Lr,ssubstr(str,p1,i-p1)); p1=i+1));
return(Lr);
}
 
{
\\ TEST
print(" *** Testing tokenize from Version #1:");
print("1.", tokenize("Hello,How,Are,You,Today",","));
\\ BOTH 2 & 3 are NOT OK!!
print("2.",tokenize("Hello,How,Are,You,Today,",","));
print("3.",tokenize(",Hello,,How,Are,You,Today",","));
}
</lang>
 
{{Output}}
<pre>
1.List(["Hello", "How", "Are", "You", "Today"])
2.List(["Hello", "How", "Are", "You", "Today", ","])
3.List([",Hello,,How,Are,You,Today,", "Hello", ",How,Are,You,Today,", "How", "Ar
e", "You", "Today"])
</pre>
 
===Version #2.===
Advanced version. Delimiter is allowed in any place. In addition, multiple delimiters are allowed too.
This is really useful for considering omitted data.
This version can be used for positional parameters processing, or for processing data from tables with string rows.
 
{{Works with|PARI/GP|2.7.4 and above}}
 
<lang parigp>
\\Tokenize a string str according to a 1 character delimiter d. Return a list of tokens.
\\ 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>1, 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("",","));
}
</lang>
 
{{Output}}
<pre>
1. 5 pp: List(["Hello", "How", "Are", "You", "Today"])
2. 5 pp: List(["Hello", "How", "Are", "You", "Today"])
3. 9 pp: List(["", "", "Hello", "", "", "How", "Are", "You", "Today"])
4. 6 pp: List(["", "", "", "", "", ""])
5. 1 pp: List([""])
6. 1 pp: List(["Hello-o-o??"])
7. 0 pp: List([""])
</pre>
 
=={{header|Pascal}}==
Anonymous user