GURGLE Tutorial: Multiple Databases and Lookup

We decide that there should be a link to a personal page from each persons generated home page. A personal page link should only be included if the person has one. This is additionally complicated because the list of people with personal pages is held in a separate data file. So the definition file needs to load multiple databases and look up information in one database from another. Each database could in principle come from a different source although we continue to use basic text files here.

First, included below is the second database source file. Copy it into a file called ppage.txt in your working directory or download ppage.txt.

FIRSTNAME|LASTNAME|HOMEPAGE
Joe|Bloggs|www.microsoft.com
Mike|Tyson|www.pgp.com
Jerry|Springer|www.livingtv.co.uk

Next we need to update the definition file. The new one is included below with the changes highlighted. Copy it into a file in the same directory as the data source file and call it home2.grg or download home2.grg.

%%database "phonedir.txt"
%%database "ppage.txt"
%%define MKDIR
%%define DELIM |
%%define NAMCOL
%%define TEXEXT .html
%%sorton %LASTNAME %FIRSTNAME
%%equate eq_init
  "/dev/null" >> _eq_outfile
%%equate eq_pre_record
  "home2/"+%FIRSTNAME+"_"+%LASTNAME+_eq_extn >> _eq_outfile
%%record
<HTML>
<HEAD>
<TITLE>%FIRSTNAME %LASTNAME</TITLE>
</HEAD>
<BODY>
<H1>%FIRSTNAME %LASTNAME</H1>
<DL>
  <DT><STRONG>JOB</STRONG></DT>
  <DD>%JOB</DD>
  <DT><STRONG>CONTACT</STRONG></DT>
  <DD>%ROOM, x#isprivate(%EXTENSION)</DD>
#dohomepage(%FIRSTNAME,%LASTNAME)
</DL>
</TABLE>
</BODY>
</HTML>
%%block HOMEPAGE
  <DT><STRONG>PERSONAL PAGE</STRONG></DT>
  <DD><A HREF="http://#eval(_homepage)/">#eval(_homepage)</A></DD>
%%equate eval()
%%equate dohomepage(f,l)
  "" >> _homepage
  roll
    if %FIRSTNAME = f and %LASTNAME = l then
      %HOMEPAGE >> _homepage
      break
    endif
  through:ppage
  if _homepage <> "" then outputs("#HOMEPAGE")
  endif
%%equate isprivate(extn)
  if extn'0 = 52 then outputs("PRIVATE")
  else outputs(extn)
  endif
%%end

There are quite a few changes to the above. First there is an extra DATABASE directive to load the new source file. Second we are creating home2/ now rather than home1/ so the output file spec has changed slightly. There is an extra line in the record that calls the dohomepage function with the current first and last names (for that record). This adds or does not add a custom block defined by the BLOCK directive based on whether the person has a home page or not. The content of the HOMEPAGE block is a standard text body but includes some references to the eval equate. This is defined as an empty equate but anything passed as argument will be processed. This essentially just allows simple inline processing. In this case all that the eval equate is used to do is return the value of a custom system variable which has the homepage set in it. Finally the dohomepage equate does the work. It uses a roll ... through loop which goes through every record in the given database, in this case ppage. Within the loop field references are against this database rather than the normal master database. The loop above tests each record until it finds a record with a matching name. When it does it sets the custom system variable (global) _homepage to that persons home page and breaks out of the loop. At the end of the equate if the home page has been set the return from the equate is a string which when reprocessed (every equate value returned is reprocessed and % or # escapes within it expanded as normal) evaluates as a call to expand the HOMEPAGE block. Phew.

Now we need to gurgle it to produce the web pages.

gurgle home2.grg

If everything worked this will produce a home2 directory and lots of separate HTML files within it. This should look like the web pages at home2/. You will see that some have personal page entries and others don't according to the content of ppage.txt.

Exercises

Increase the data space and substitute a real phone directory!

Next, brief notes on using an RDBMS and further examples.

NEXT