Case Study CS00002:Extract Table of Contents from Text File

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.

  1. The file name is "AesopsFables.txt"
  2. The file is in the "CaseStudies" directory
  3. The "CaseStudies" directory is normally in "C:\GLEE\Website"
  4. The table begins with the text "TABLE OF CONTENTS:"
  5. Right after the table are some blank lines and then the words "Aesop's Fables"

The solution:

  1. Create a file context "fcDir" for the "CaseStudies" directory
  2. Create a file context "fcPath" for the location of the "CaseStudies" directory (probably "C:\GLEE\Website"
  3. Create a file object "f" for the AesopsFables.txt file
  4. Read the contents of the file into a string variable "t"
  5. Save the beginning target ("TABLE OF CONTENTS:") in the variable "begin";
  6. Save the ending target ("Aesop's Fables") in the variable "end"
  7. Move the "t" stream-cursor to the location of "begin"
  8. Read the "t" stream to the location of "end" and save back in "t"
  9. Drop "end" from the end of "t"
  10. Drop line feeds from end of "t"
  11. Display "t"

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.

  1. The ~> Delete trailing operator drops the characters given in its right argument (in this case 13 10 #asc ) specifies the return and line feed characters.
  2. $; Displays the results and completes the line.

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.