The problem: We have a text file containing the Aesop's Fables. At the beginning of that file is the table of contents. We want to extract and display it.
The solution:
Note: You can cut and paste these code fragments into the code pane of the Glee interpreter and experiment as you go along to see the actual operations live.
The Glee code:
$$ Define Input Data:
"TABLE OF CONTENTS"=> begin;
"Aesop's Fables"=> end;
"C:\GLEE\Website"=> path;
"CaseStudies\"=> dir;
"AesopsFables.txt" => file;
$$ Set file contexts
path #fc =>fcPath;
dir #fc => fcDir;
$$ Define file object and read contents
file #file (fcPath,fcDir) => f;
f[]=>t;
$$ Extract just the data we want
t @->>&begin;
t ->>&end => t;
t ~>(13 10 #asc)=>t$;
The Output:
TABLE OF CONTENTS: | |
The Cock and the Pearl | The Frog and the Ox |
The Wolf and the Lamb | Androcles |
The Dog and the Shadow | The Bat, the Birds, and the |
......
.....
The Wind and the Sun | The Buffoon and the Countryman |
Hercules and the Waggoner | The Old Woman and the Wine-Jar |
The Man, the Boy, and the Donkey | The Fox and the Goat |
The play-by-play:
"TABLE OF CONTENTS"=> begin;
"Aesop's Fables"=> end;
"C:\GLEE\Website"=> path;
"CaseStudies\"=> dir;
"AesopsFables.txt" => file;
Note: This is overkill but it illustrates separation of data from code making maintenance of code a little easier.
path #fc =>fcPath;
dir #fc => fcDir;
Create file contexts for the components of the file location. By
doing this, if the file path or directory changes we can quickly change
references for its new location. Note: This again is overkill but
illustrates separating context definition from code which can reduce
maintenance issues later on.
file #file (fcPath,fcDir) => f;
The file object "f" is a
function of the file name "file" and the two file context components
("fcPath" and "fcDir")which we catenate together to makeup
the full path to the file.
f[]=>t;
[] Indexing
out of a file object with a null index reads the complete contents of
the file. This we store in the variable t. The streaming cursor position begins at location
0.
t @->>& begin;
In @->>&
"@" means
At cursor and ->> means stream
forward and & means
all. So @->>&
means move forward to the
position where all characters (in the string begin) are matched and set
the cursor at that position. If it doesn't find a match, this
moves the cursor to the end of the stream.
t ->>& end => t;
In ->>& we have everything
except the @ in the operator before.
Thus, we read characters until the string end is reached (but not including the string),
rather than just moving the cursor. We replace t with the characters read.
t ~>(13 10
#asc)=>t$;
Drop the trailing empty lines from the end of t.
This completes the example. To better understand these operators and other things you can do with them, consult the operator pages according to the type of data you see being operated on.