User:AnatolV/Helper Functions: Difference between revisions

m
→‎String Functions: a duplicate deleted
m (→‎String Functions: a duplicate deleted)
Line 441:
polar2(x,y)={my(v,r,a); r=sqrt(x^2+y^2); a=atan(y/x); return([r,a])}
</lang>
==String Functions==
The first 4 are already here on RC. A few others I will keep on hold (hoping to create tasks later).
 
<lang parigp>
\\ SF#1 ssubstr(): Returns the substring of the string str specified by the start
\\ position s and a length n. If n=0 then to the end of str. 3/5/16
ssubstr(str,s=1,n=0)={
my(vt=Vecsmall(str),ve,vr,vtn=#str,n1);
if(vtn==0,return(""));
if(s<1||s>vtn,return(str));
n1=vtn-s+1; if(n==0,n=n1); if(n>n1,n=n1);
ve=vector(n,z,z-1+s); vr=vecextract(vt,ve); return(Strchr(vr));
}
 
\\ SF#2 stok(): Tokenize a string str according to 1 character delimiter d.
\\ Return a list of tokens. 3/5/16
\\ Note: It is using ssubstr().
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);
}
 
\\ SF#3 sreverse(): Return the reversed string str. 3/3/2016
sreverse(str)={return(Strchr(Vecrev(Vecsmall(str))))}
 
\\ SF#4 srepeat(): Repeat a string str the specified number of times ntimes
\\ and return composed string. 3/3/2016
srepeat(str,ntimes)={
my(srez=str,nt=ntimes-1);
if(ntimes<1||#str==0,return(""));
if(ntimes==1,return(str));
for(i=1,nt, srez=concat(srez,str));
return(srez);
}
 
\\ SF#5 <<coming soon>>
</lang>
 
=gnuplot Helper File-Functions=
File for the load command is the only possible imitation of the fine function in the '''gnuplot'''.<br><br>
Anonymous user