GURGLE Tutorial: Sorting and Functions

A couple of extra things are added now. We want the output to be be sorted alphabetically by name for one. The other thing we want to do is add a check onto the extension number - if it starts with a 4 its a private extension so we don't want it shown on the output. Included below is the definition file to do all this. Copy it into a file in the same directory as the data source file and call it phones3.grg or download phones3.grg.

%%database "phonedir.txt"
%%define DELIM |
%%define NAMCOL
%%define TEXEXT .lst
%%sorton %LASTNAME %FIRSTNAME
%%header
PHONE DIRECTORY
%%record
 
  NAME:     %FIRSTNAME %LASTNAME
  JOB:      %JOB
  CONTACT:  %ROOM, x#isprivate(%EXTENSION)
%%end
%%equate isprivate(extn)
  /* if the first character of the extension is the number 4 (in ascii)
     then return "PRIVATE" else return the extension */
  if extn'0 = 52 then outputs("PRIVATE")
  else outputs(extn)
  endif

The SORTON directive sorts by the first field and then by the second field within each sorted first field group etc. Any # in a text body is expanded by the result of calling the given function (with arguments in this case). The equate function definition itself should be fairly self explanatory. This tutorial doesn't go into any real detail about the internal processing language itself, see the user manual for more on this.

Now we need to gurgle the definition file and produce the output.

gurgle phones3.grg

If everything worked this will create phones3.lst which is the output file and should look like below.

PHONE DIRECTORY
 
  NAME:     Rosanne Barr
  JOB:      Receptionist
  CONTACT:  A01, x1111
 
  NAME:     Joe Bloggs
  JOB:      Manager
  CONTACT:  C99, xPRIVATE
 
  NAME:     Bill Clinton
  JOB:      Legal Services
  CONTACT:  C55, xPRIVATE
 
  NAME:     Frasier Crane
  JOB:      Counsellor
  CONTACT:  B78, x6542
 
  NAME:     John Doe
  JOB:      Coroner
  CONTACT:  B56, x3888
 
  NAME:     Jack Frost
  JOB:      Air Conditioning Technician
  CONTACT:  A23, xPRIVATE
 
  NAME:     Jerry Springer
  JOB:      Public Relations
  CONTACT:  B44, x8844
 
  NAME:     Mike Tyson
  JOB:      Security
  CONTACT:  A23, x7911

As you can see the output is now sorted and private numbers have been hidden.

Exercises

Try different sorts and change the equate to disguise different numbers or to disguise all office locations on C floor for example.

Next we'll turn our report into a more user friendly command.

NEXT