{VERSION 6 0 "IBM INTEL LINUX" "6.0" } {USTYLETAB {CSTYLE "Maple Input" -1 0 "Courier" 0 1 255 0 0 1 0 1 0 0 1 0 0 0 0 1 }{PSTYLE "Normal" -1 0 1 {CSTYLE "" -1 -1 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 }0 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "Heading 1" 0 3 1 {CSTYLE "" -1 -1 "" 1 18 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }1 0 0 0 8 4 0 0 0 0 0 0 -1 0 }{PSTYLE "" 0 256 1 {CSTYLE "" -1 -1 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 }3 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE " " 3 257 1 {CSTYLE "" -1 -1 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 }3 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }} {SECT 0 {EXCHG {PARA 257 "" 0 "" {TEXT -1 34 "Handling ASCII text file s in Maple" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 256 "" 0 "" {TEXT -1 56 "Mike O'Sullivan\nSan Diego State University\nJanuary 2005\n" }} }{EXCHG {PARA 0 "" 0 "" {TEXT -1 82 "This worksheet contains some fun ctions that can be used to encrypt a text file. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 109 "Text files are s tored as ASCII text. We use readbytes(textfile, infinity) to read the whole file, producing " }}{PARA 0 "" 0 "" {TEXT -1 82 "a list of numb ers between 0 and 127 each representing a characters in the text. " }}{PARA 0 "" 0 "" {TEXT -1 77 "We use writebytes(outfile, ls) to write the data in ls to the file outfile. " }}{PARA 0 "" 0 "" {TEXT -1 43 "Also flush(); is used to enforce the write." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 74 "For one way to handle ASCII tex t see the final example in simple_text.mws." }}{PARA 0 "" 0 "" {TEXT -1 327 "Here, I've created lists for the numbers corresponding to capi tal and ordinary letters (Majletters[], Minletters), the digits 0 to 9 (Numberlist[]), the space, tab and newline (Spacelist[]), the punctu ation marks (Punctlist[]), other symbols (Otherlist[]) and for the num bers that do not correspond to characters (Badlist[]). " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 304 "I read from a file, trim out all numbers corresponding to punctuation, and then covert sp ace characters to 0 and letters to a number between 1 and 26. One can then use a cipher to encrypt.\nI just converted back to ascii numbers for the capital letters and wrote to a file, to show how the process \+ works." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "restart;" }}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 36 "Maki ng lists of letters numbers etc." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 118 "badlist:=[]:\nfor i from 1 to 8 do badlist:= [op(badlist),i] \+ od: \nfor i from 12 to 31 do badlist := [op(badlist),i] od:" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 24 "spacelist:=[9,10,11,32]:" }} }{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 31 "punctlist:=[33,44,46,58,59, 63]:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 45 "numberlist:= [48,49 ,50,51,52,53,54,55,56,57]:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 73 "majletters:=[]:\nfor i from 65 to 90 do majletters:=[op(majletters ),i] od:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 76 "minletters := [ ]:\nfor i from 97 to 122 do minletters:=[op(minletters),i] od:" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 153 "otherlist:=[]:\nfor i from \+ 34 to 43 do otherlist:=[op(otherlist),i] od:\notherlist:= [ops(otherli st),45,47, 60,61,62,64,91,92,93,94,95,96,123,124,125,126]:" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 413 "The function trim1(text) takes a string \+ of ASCII numbers and produces a list of numbers from 0 to 26. It trim s out the non-characters, the symbols, the digits and the punctuation \+ marks. It replaces all space characters with 0 and any letter (whethe r capital or ordinary ) with the corresponding number between 1 and 26 . The function can be easily changed to accomodate puctuation marks o r digits, or whatever. " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 350 "trim1:= proc(text)\nlocal i, outtext;\n\nouttext:=[];\nfor i from 1 t o nops(text) do\n if member(text[i],spacelist) then \n outtext := [op(outtext),0]; \n elif member(text[i], majletters) then \n outt ext := [op(outtext),text[i]-64]: \n elif member (text[i], minletters) then \n outtext := [op(outtext), text[i]-96]:\n fi;\nod;\nRETURN( outtext);\nend;" }}}{EXCHG {PARA 0 "" 0 "" {TEXT -1 114 "This function inverts, as well as possible, the function trim1(). It takes a list \+ of numbers---all of which must " }}{PARA 0 "" 0 "" {TEXT -1 116 "be be tween 0 and 26 (!)--- and produces ASCII text for either space (32) or for a capital letter. If the trim1() is" }}{PARA 0 "" 0 "" {TEXT -1 57 "modified, ascii() must be appropriately modified as well." }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 211 "ascii1 := proc(text)\nlocal i, outtext;\n\nouttext:=[]:\nfor i from 1 to nops(text) do\n if (tex t[i]=0) then outtext := [op(outtext),32]; \n else outtext := [op(out text),text[i]+64];\n fi;\nod;\nRETURN(outtext);\nend;" }}}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 28 "Using readfi le and writefile" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 109 "Here is an ex ample. Make a textfile in the current directory named \"textfile\" wh ich has the following text, " }}{PARA 0 "" 0 "" {TEXT -1 37 "\"This is a text file with 1 number.\"" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "" 0 "" {TEXT -1 109 "We then apply trim1() and then ascii(). \+ The latter two are shorter because the period was removed by trim(). " }}{PARA 0 "" 0 "" {TEXT -1 65 "Finally we write to \"outfile\". The n file \"outfile\" has the text." }}{PARA 0 "" 0 "" {TEXT -1 33 "\"THI S IS A TEXT FILE WITH NUMBER\"" }}{PARA 0 "" 0 "" {TEXT -1 71 "You nee d to use fflush to force the output to be written into the file." }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 84 "Also the \+ exact location of the textfile on your computer may have to be specifi ed. " }}{PARA 0 "" 0 "" {TEXT -1 14 "See Help:file." }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 39 "H:= readbytes(\"./textfile\", infinity); \+ " }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 14 "HH:= trim1(H);" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "HHH:=ascii1(HH);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 29 "nops(H); nops(HH); nops(HHH);" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 49 "writebytes(\"./outfile\",HHH ); fflush(\"./outfile\");" }}}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{MARK "9" 0 }{VIEWOPTS 1 1 0 1 1 1803 1 1 1 1 }{PAGENUMBERS 0 1 2 33 1 1 }