character - ascii in Maple V
The following text is a set which can be used to
build an interpreter or similar ...(works also with older Maple V releases)
This work is done in order to build an
interpreter inside of Maple, as is already done
for
Dirac Notation Interpreter
in MATLAB
Save it and use "read filename" to enter it.
test samples are at the end
And here is the test with results - in MAPLE dialog
# A set of functions to handle ASCII characters and strings
# in Maple V release 3
# uses substring, not available before
#
# L. Kocbach, Dept. Physics, University Bergen, NORWAY
# ladi@sparc-atom.fi.uib.no
#
#==================================================
#
# FUNCTIONS:
#
# tolist() to_ascii() to_char() to_chars()
# ListListPos() ListReplace()
# searchstring() stringrep()
#
# USAGE:
#
# LIST_OF_CHARS := tolist( STRING );
# STRING_LENGTH_1 := to_char ( NUMBER );
# STRING := to_chars ( LIST_OF_NUMBERS );
# LIST_OF_NUMBERS := to_ascii( STRING ) ;
#
# Strings are best treated as lists of ascii values.
# For positions of a sublist in a list
# and for replacement of a sublist by a new one in a list:
#
# LIST_OF_POSITIONS := ListListPos(LIST,SUBLIST)
# NEW_LIST := ListReplace(LIST,SUBLIST,NEWSUBLIST)
#
# These are applied to strings using to_ascii
# and back to strings using to_chars :
#
# searchstring( STRING, SUBSTRING );
# stringrep( STRING, SUBSTRING, NEW_SUBSTRING );
#==================================================
tolist := proc(minstr)
local vv,zz,qq;
#
# convert a string to a list - character by character
#
vv := length(minstr);
zz := [];
for qq to vv do
zz := [op(zz),substring(minstr,qq .. qq)]
od
end;
#==================================================
#
# Constructs a global ASCII value table
#
alias(Su=substring);
outlist:=[ ]:
st1:=`!"#$%&'()*+,-./`:
st2:=`:;<=>?@`:
st3:=`[]^_```:
st4:=`{|}~`:
st5:=`0123456789`:
Lst:=tolist(st5):
TableOfAscii:=table([
Su(st1, 1.. 1)= 33,Su(st1, 2.. 2)= 34,Su(st1, 3.. 3)= 35,\
Su(st1, 4.. 4)= 36,Su(st1, 5.. 5)= 37,Su(st1, 6.. 6)= 38,\
Su(st1, 7.. 7)= 39,Su(st1, 8.. 8)= 40,Su(st1, 9.. 9)= 41,\
Su(st1, 10.. 10)= 42,Su(st1, 11.. 11)= 43,Su(st1, 12.. 12)= 44,\
Su(st1, 13.. 13)= 45,Su(st1, 14.. 14)= 46,Su(st1, 15.. 15)= 47,\
Su(st2, 1.. 1)= 58,Su(st2, 2.. 2)= 59,Su(st2, 3.. 3)= 60,\
Su(st2, 4.. 4)= 61,Su(st2, 5.. 5)= 62,Su(st2, 6.. 6)= 63,\
Su(st2, 7.. 7)= 64,\
Su(st3, 1.. 1)= 91,Su(st3, 2.. 2)= 93,Su(st3, 3.. 3)= 94,
Su(st3, 4.. 4)= 95,Su(st3, 5.. 5)= 96,\
Su(st4, 1.. 1)= 123,Su(st4, 2.. 2)= 124,\
Su(st4, 3.. 3)= 125,Su(st4, 4.. 4)= 126,\
'A'=65,'B'=66,'C'=67,'D'=68,'E'=69,'F'=70,'G'=71,\
'H'=72,'I'=73,'J'=74,'K'=75,'L'=76,'M'=77,'N'=78,\
'O'=79,'P'=80,'Q'=81,'R'=82,'S'=83,'T'=84,'U'=85,\
'V'=86,'W'=87,'X'=88,'Y'=89,'Z'=90,\
'a'=97,'b'=98,'c'=99,'d'=100,'e'=101,'f'=102,\
'g'=103,'h'=104,'i'=105,'j'=106,'k'=107,'l'=108,\
'm'=109,'n'=110,'o'=111,'p'=112,'q'=113,\
'r'=114,'s'=115,'t'=116,'u'=117,\
'v'=118,'w'=119,'x'=120,\
'y'=121,'z'=122,'` `'=32,\
0 = 48,1 = 49,2 = 50, 3=51,4=52,\
5=53,6=54,7=55,8=56,9=57,\
Lst[1]= 48 , Lst[2]= 49, Lst[3] = 50 , Lst[4] = 51, Lst[5 ]= 52,\
Lst[6]= 53 , Lst[7]=54, Lst[8] = 55, Lst[9 ] = 56, Lst[10]= 57 ]):
#==================================================
#
# Constructs a global CHARACTER table
#
emptstr:=` `:
st1:=`!"#$%&'()*+,-./`:
st2:=`:;<=>?@`:
st3:=`[ ]^_```:
st4:=`{|}~`:
st5:=`0123456789`:
strsmall:=`abcdefghijklmnopqrstuvwxyz`:
strcapit:=`ABCDEFGHIJKLMNOPQRSTUVWXYZ`:
alphabet:=cat(emptstr,st1,st5,st2,strcapit,st3,strsmall,st5):
to_charTable:=tolist(alphabet):
#==================================================
to_char := proc(ascnumber)
#
# convert an ASCII code to a character
#
RETURN ( to_charTable[ascnumber] )
end;
#==================================================
to_chars := proc(asclist)
local strres,ch, zz;
#
# convert a list of ASCII codes to a string
#
zz:=map(to_char,asclist);
strres:=` `;
for ch in zz do
strres:=cat(strres,ch);
od;
RETURN(strres)
end;
#==================================================
to_ascii:=proc(st)
local ll,nn,ss,ii,num, outlist,st1,st2,st3,st4,st5,Lst;
#
# convert a string to a list of ASCII codes
#
outlist:=[ ];
ll:=length(st);
if ll= 0 then
RETURN(0)
fi;
nn:=1;
for ii from 1 to ll do
nn:=substring(st,ii..ii);
ss:=TableOfAscii[nn];
outlist:=[ op(outlist),ss];
od;
RETURN(outlist)
end;
### testlist:=to_ascii(`0123456789`);
### to_chars(testlist);
#=====================================
ListListPos := proc(testlist,objektlist)
local poslist, kk, ListLen,ObjLen,stillequal,mainpoint;
mainpoint:=0;
ListLen:=nops(testlist);
ObjLen:=nops(objektlist);
#
poslist:=[]:
while mainpoint