MAT 331: Reading Maple from the Web

It is not uncommon to need to access some maple code from the class web page. For example, the data for the first project is stored on the class web page.

Unlike the case of a maple worksheet (for which you can just use Open URL... from the menu), opening a file from the web is a little trickier. As an example, we will assume that we want to read the data from the web address

http://www.math.sunysb.edu/~scott/mat331.spr13/problems/electron.txt

Here are some ways you can accomplish this.

Cut and Paste
You can open the page with the data in your web browser, then use highlight the relevant stuff and copy it to the clipboard, then paste into a worksheet. This is easy, but error-prone, and tedious if there is a lot of data.

Download file
You can right-click on the link to the file and select Save Link As... or Save Target As... to save this to a file on your local computer. You should take note of the name and location of the file you saved. For example, if you saved the above link to your desktop, the full path might be something like D:\Users\netid\Desktop\electron.txt.

Now, in maple, issue a command like
read("D:/Users/netid/Desktop/electron.txt");
This will execute the commands in the electron.txt file.

Note that when typing the name of the file on a windows system, you will need to either write a forward slash (/) instead of the backslashes (\) in the name, or double the backslashes, as in D:\\Users\\netid\\Desktop\\electron.txt.

Read directly from the web
You can read the file directly into maple using the HTTP library.
with(HTTP):
URL:="http://www.math.sunysb.edu/~scott/mat331.spr13/problems/electron.txt";
status,webfile,headers:=Get(URL): Code(status);

n:=0:
while (n < length(webfile)) do
 parse(webfile,statement,lastread='n', offset=n);
od:
Below is a maple procedure which implements this in a slightly more compact way (which you can download here, or paste from this page.
ReadFromWeb:=proc(URL::string, {printfile::truefalse:=false})
 local n,m, status, webfile, headers;
 status,webfile,headers:=HTTP[Get](URL):
 if ( HTTP[Code](status) <> "OK") then
     error(HTTP[Code](status),URL);
 fi;
 # now read the web page
 n:=0:
 while (n < length(webfile)) do
   m:=n;
   parse(webfile,statement,lastread='n', offset=n);
   if (printfile) then printf("%s",webfile[m+1..n]); fi;
 od:
end:
Note that the above routine takes an optional argument printfile, which, if included, prints out the statements that Maple is parsing.