Go to the first, previous, next, last section, table of contents.


Examples

Included below is a complete example GRG file of a reasonable complexity. This demonstrates how moderately complex output can be generated without doing any very complex equate work (ie. just by using basic conditions on fields).

%% DAI Grants Awarded Report Form Template
%%DEFINE    TITLE Grants Awarded
%%DATABASE  "../research.dbf"
%%FILTER    "%GRANT_STAT=ACTIVE" "%GRANT_STAT=AWARDED"
%%SORTON    %AI_AREA %PI_SNAME
%%EQUATE    GRANT_HOLDER ?CO_INVEST"; %CO_INVEST"
%%EQUATE    DURATION ?START_DATE"%START_DATE--"%END_DATE
%%EQUATE    ACCOUNT_NO ?ACCOUNT_NO"RR%ACCOUNT_NO"
%%EQUATE    RAS_PER_YR ?RAS_PER_YR
%%EQUATE    STAFF_AD ?STAFF_AD"%STAFF_AD "?AD_PERCENT"%AD_PERCENT\%; "
%%EQUATE    STAFF_TG ?STAFF_TG"%STAFF_TG "?TG_PERCENT"%TG_PERCENT\%; "
%%EQUATE    STAFF_CN ?STAFF_CN"%STAFF_CN "?CN_PERCENT"%CN_PERCENT\%"
%%EQUATE    STAFF_AR ?STAFF_AR"%STAFF_AR "?AR_PERCENT"%AR_PERCENT\%"
%%EQUATE    STAFF1 ?STAFF_NM1
%%EQUATE    STAFF2 ?STAFF_NM2"; %STAFF_NM2"
%%EQUATE    STAFF3 ?STAFF_NM3"; %STAFF_NM3"
%%EQUATE    STAFF4 ?STAFF_NM4"; %STAFF_NM4"
%%EQUATE    STAFF5 ?STAFF_NM5"; %STAFF_NM5"
%%HEADER
  \documentstyle[grants]{article}
  \pagestyle{headings}
  \markright{\noindent Department of Artificial Intelligence
  \hfill %%TITLE as at \today{} \hfill}
  \sloppy
  \begin{document}
  \begin{titlepage}
  \mbox{}
  \end{titlepage}
%%PAGE01 3
  \vspace*{1ex}
  \centerline{{\bf DEPARTMENT OF ARTIFICIAL INTELLIGENCE}}
  \centerline{{\bf %%TITLE as at \today}}
  \vspace*{1ex}
%%PAGENN 4
  \vspace*{1ex}
%%BANNER    %AI_AREA
  \vspace*{1ex}
  \flushleft{\underline{\bf %AI_AREA}}
%%RECORD
  \begin{list}{}{\leftmargin 2.15in
  \renewcommand{\makelabel}[1]{\hfil \#1}\labelwidth 2.1in
  \itemsep 0ex \parsep 0ex}
  \item[{\bf Title:}] %GRANT_TITL
  \item[{\bf Grantholder:}] %PI_SNAME, %PI_INITS%%GRANT_HOLDER
  \item[{\bf Grant No:}] %GRANT_NO
  \item[{\bf A/C No:}] %%ACCOUNT_NO
  \item[{\bf Duration:}] %%DURATION
  \item[{\bf Value:}] \pounds%TOTAL\\{\bf Staff} \pounds%STAFF_COST;
  {\bf Travel} \pounds%TRAVEL; {\bf Consumables} \pounds%CONSUMABLE;
  \\{\bf Equipment} \pounds%EQUIPMENT; {\bf Indirect} \pounds%INDIRECT
  \item[{\bf RA Man Yrs/Ac Yr:}] %%RAS_PER_YR
  \item[{\bf Research Posts:}] %%STAFF_AR
  \item[{\bf Other Posts:}] %%STAFF_AD%%STAFF_TG%%STAFF_CN
  \item[{\bf Staff:}] %%STAFF1%%STAFF2%%STAFF3%%STAFF4%%STAFF5
  \item[{\bf Source Of Funding:}] %FUNDING
  \end{list}
%%FOOTER
  \end{document}

The example below is one of the simplest files that could be created. This makes an SQL query to a RDBMS and just dumps the output in delimited form using the default header and record text bodies.

%%DATABASE "cat.sql"
%%DEFINE PHYSDB animals
%%DEFINE NAMCOL
%%DEFINE DELIM |

The example below shows a more complex SQL query which is then used to construct HTML for a web page in a fairly simple way. The file includes some other files for default setup which are not shown below.

#!/usr/local/bin/grg
%%INCLUDE   "../PTC/html.pt"
%%INCLUDE   "../PTC/basic.grg"
%%INCLUDE   "banner.html"
%%EQUATE    eq_texinit "../testing/absences.html" >>_eq_outfile
%%DEFINE    PHYSDB daidb
%%DEFINE    NAMCOL

%%DATABASE	"absence.sql"
  select
    lastname, firstname, reason, reason_oth, start_date as sdate,
    return_date as rdate,
    date_part('year',start_date) as year,
    date_trunc('month',start_date) as month,
    return_date - start_date as days
  from
    absence, person
  where
    person.person# = absence.person# and
    ( ( start_date >= date('today') and
        start_date < date('today') + '28 days' ) or
      ( start_date < date('today') and
        return_date >= date('today') ) )
  order by
    lastname

%%HEADER
  #BEGIN_INSERT
  <TABLE BORDER=0>
  <TR ALIGN=LEFT>
    <TH COLSPAN=2>Name
    <TH COLSPAN=2>Date(s)
    <TH COLSPAN=2>Reason

%%RECORD
  <TR>
    <TD><B>%FIRSTNAME %LASTNAME</B><TD>
    <TD>%SDATE #EVAL(%RDATE<>""?" to "+%RDATE:;) (%DAYS)<TD>
    <TD>#EVAL(%REASON="Other"?%REASON_OTH:%REASON;)

%%FOOTER
  </TABLE>
  #END_INSERT

%%END

The example below just contains some useful equate definitions for commonly required functions (particularly string handling).

%%EQUATE traceon
  /* turn on equate tracing */
  1 >> _eq_trace
%%EQUATE traceoff
  /* turn off equate tracing */
  0 >> _eq_trace
%%EQUATE datey
  /* returns string with century and year part of date */
  inputs(s)
  outputs(substr(s,0,3))
%%EQUATE datem
  /* returns string with month part of date */
  inputs(s)
  outputs(substr(s,4,5))
%%EQUATE dated
  /* returns string with day part of date */
  inputs(s)
  outputs(substr(s,6,7))
%%EQUATE bdate
  /* returns string with date formatted to "dd/mm/ccyy" */
  inputs(s)
  outputs(dated(s)+"/"+datem(s)+"/"+datey(s))
%%EQUATE ltou
  /* converts lower case string s to upper case */
  inputs(s)
  if not (s = "") then
    0 >> i
    while s'i <> 0 do
      if s'i >= 97 and s'i <= 122 then
        s,(s'i - 32)`i >> s
      endif
      ++i >> i
    wend
  endif
  outputs(s)
%%EQUATE substr
  /* returns substring of s1 starting at s and ending at e */
  inputs(s1,s,e)
  "" >> s2
  0 >> x
  while (s <= e) do
    s2,(s1's)`x >> s2
    ++s >> s
    ++x >> x
  wend
  outputs(s2,0`x)
%%EQUATE strlen
  /* returns length of string s */
  inputs(s)
  0 >> x while s'x <> 0 do ++x >> x wend
  outputs(x)
%%EQUATE nrecs
  /* returns size (n records) of given database db */
  /* note that for a quoted string keywords are not expanded within so
  the raw reversed operators must be used instead */
  inputs(db)
  0 >> n
  if db = "" then "" >> s else ":" + db >> s endif
  "(n + 1 >> n)" + s >> s
  exec(s)
  outputs(n)


Go to the first, previous, next, last section, table of contents.