<?xml version="1.0" encoding="ISO-8859-1"?>
<html xmlns="http://www.w3.org/1999/xhtml"><head xmlns="http://www.w3.org/1999/xhtml"><title xmlns="http://www.w3.org/1999/xhtml">Inter-Records scripts</title><meta xmlns="http://www.w3.org/1999/xhtml" name="generator" content="DocBook XSL Stylesheets V1.50.0"/><link rel="home" href="index.html" title="Pilot-DB Users Manual"/><link rel="up" href="ch09.html" title="Chapter 9. Scripts"/><link rel="previous" href="ch09s03.html" title="Operators"/><link rel="next" href="ch10.html" title="Chapter 10. Using a Plugin"/></head><body><div xmlns="http://www.w3.org/1999/xhtml" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Inter-Records scripts</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch09s03.html">Prev</a> </td><th width="60%" align="center">Chapter 9. Scripts</th><td width="20%" align="right"> <a accesskey="n" href="ch10.html">Next</a></td></tr></table><hr/></div><div xmlns="http://www.w3.org/1999/xhtml" class="section"><div xmlns="http://www.w3.org/1999/xhtml" class="titlepage"><div xmlns="http://www.w3.org/1999/xhtml"><h2 class="title" style="clear: both"><a xmlns="http://www.w3.org/1999/xhtml" id="id2832245"/>Inter-Records scripts</h2></div></div><fake><p xmlns="http://www.w3.org/1999/xhtml">
     Basic Scripts operate on fields within a single record, but it is
     also possible to construct scripts that make use of information in
     other records.  Scripts that refer to data in more than one record are
     called inter-record scripts.  Inter-record scripts are defined just as
     basic scripts with two important differences.  The most important of
     these differences, is the use of two special operators: <tt xmlns="http://www.w3.org/1999/xhtml">ffr</tt> and
     <tt xmlns="http://www.w3.org/1999/xhtml">sort</tt>. Simply using these operator in your script changes the
     script's type from a basic script to an inter-record script.
    </p></fake><div xmlns="http://www.w3.org/1999/xhtml" class="informaltable"><table xmlns="http://www.w3.org/1999/xhtml" border="1"><colgroup xmlns="http://www.w3.org/1999/xhtml"><col xmlns="http://www.w3.org/1999/xhtml"/><col xmlns="http://www.w3.org/1999/xhtml"/><col xmlns="http://www.w3.org/1999/xhtml"/></colgroup><thead><tr xmlns="http://www.w3.org/1999/xhtml"><th align="left"><fake><p xmlns="http://www.w3.org/1999/xhtml">Operator &amp; Args</p></fake></th><th align="left"><fake><p xmlns="http://www.w3.org/1999/xhtml">Purpose</p></fake></th><th align="left"><fake><p xmlns="http://www.w3.org/1999/xhtml">Explanation</p></fake></th></tr></thead><tbody xmlns="http://www.w3.org/1999/xhtml"><tr xmlns="http://www.w3.org/1999/xhtml"><td align="left">sort [fld] [up?]</td><td align="left"><fake><p xmlns="http://www.w3.org/1999/xhtml">Sort</p></fake></td><td align="left"><fake><p xmlns="http://www.w3.org/1999/xhtml">2 arguments, each of which
                                        must be an integer.  The first 
                                        argument determines what field to
                                        use for sorting.  The second 
                                        argument is 0 if the records 
                                        should be sorted from high to low
                                        (descending)</p></fake></td></tr><tr xmlns="http://www.w3.org/1999/xhtml"><td align="left">ffr [fld] [off] [d]</td><td align="left"><fake><p xmlns="http://www.w3.org/1999/xhtml">Field From Record</p></fake></td><td align="left"><fake><p xmlns="http://www.w3.org/1999/xhtml">2 or 3 arguments, the first 2 of 
                                        which must be an integer.  The first
                                        argument is the field to be retrieved.
					(As in the design view, fields are
					numbered beginning with 0).  The
					second argument is the record offset.
					That is, the relative location
                                        of the target record from the
                                        current record. If a third argument
					is present, it is used as a default
					value for this function if or when 
					the specified record does not exist 
					(as may often happen for the first 
					record in the sort order).</p></fake></td></tr></tbody></table></div><fake><p xmlns="http://www.w3.org/1999/xhtml">
     The second important difference between an inter-record script and
     a basic script is that an inter-record script is made of TWO distinct
     parts.  The first part is a single sort clause.  The second part
     contains one or more additional clauses and is executed once for each
     record in the database.
    </p></fake><fake><p xmlns="http://www.w3.org/1999/xhtml">
     Here is an example of an inter-record script:
    </p></fake><pre xmlns="http://www.w3.org/1999/xhtml" class="screen">(sort  1 1)(+ %0 (ffr 1 1))</pre><fake><p xmlns="http://www.w3.org/1999/xhtml">
     This script sorts first sorts the database so that field 1 is
     ascending in value.  Then for each record, it adds the value of field
     0 in the current record to the value of field 1 in the next record.
    </p></fake><fake><p xmlns="http://www.w3.org/1999/xhtml">
     The sort clause ensures that inter-record scripts will calculate the
     expected values even when records are temporarily arranged in an
     unexpected order.
    </p></fake><div xmlns="http://www.w3.org/1999/xhtml" class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><fake><p xmlns="http://www.w3.org/1999/xhtml">
      In general, you cannot use references to inter-record scripts
      within another script.  A special case, however, does allow
      circumventing this generality and comes in useful on occasion.
      When the 'ffr' function is used with a default value, it can be used
      to reference any field in the database.  This makes possible operations
      such as accumulation for a specific type of record:
     </p></fake><fake><p xmlns="http://www.w3.org/1999/xhtml">
      assume the database has three fields: [string] [int] [calc] the
       string is a person's name, the int is how much money they
       borrow on a particular occasion, and we want to have a running
       total of the amount each person owes at any given time.
     </p></fake><pre xmlns="http://www.w3.org/1999/xhtml" class="screen">(sort 0 1) (+ (? (seq (ffr 0 -1 na) %0) (ffr 2 -1 0) 0) %1)</pre><fake><p xmlns="http://www.w3.org/1999/xhtml">
      This script deserves some explanation:
     </p></fake><table xmlns="http://www.w3.org/1999/xhtml" class="simplelist" border="0" summary="Simple list"><tr xmlns="http://www.w3.org/1999/xhtml"><td xmlns="http://www.w3.org/1999/xhtml">
       The first clause &quot;(sort 0 1)&quot; sorts the data by the person's
         name (field 0).
      </td></tr><tr xmlns="http://www.w3.org/1999/xhtml"><td xmlns="http://www.w3.org/1999/xhtml">
       The clause &quot;(ffr 0 -1 na)&quot; gets the name from the
         previous record, or uses the string &quot;na&quot; if there is no
         previous record.
      </td></tr><tr xmlns="http://www.w3.org/1999/xhtml"><td xmlns="http://www.w3.org/1999/xhtml">
       The clause &quot;(seq (ffr 0 -1 na) %0)&quot; compares the name
         in the previous record with the name in the current record.
      </td></tr><tr xmlns="http://www.w3.org/1999/xhtml"><td xmlns="http://www.w3.org/1999/xhtml">
       The clause &quot;(? (seq (ffr 0 -1 na) %0) (ffr 2 -1 0) 0)&quot;
         returns the accumulator's value (field 2) in the previous
         record if &quot;(seq (ffr 0 -1 na) %0)&quot; is true.  Otherwise
         it returns 0.  Note also, that it returns 0 in the special case
         when the previous record doesn't exist.
      </td></tr><tr xmlns="http://www.w3.org/1999/xhtml"><td xmlns="http://www.w3.org/1999/xhtml">
       The clause &quot;(+ (? ... ) %1)&quot; adds the value described above
         with the amount borrowed (field 1) in the current record.
      </td></tr></table><fake><p xmlns="http://www.w3.org/1999/xhtml">
      Here is what a sample data base might look like:
     </p></fake><div xmlns="http://www.w3.org/1999/xhtml" class="informaltable"><table xmlns="http://www.w3.org/1999/xhtml" border="1"><colgroup xmlns="http://www.w3.org/1999/xhtml"><col xmlns="http://www.w3.org/1999/xhtml"/><col xmlns="http://www.w3.org/1999/xhtml"/><col xmlns="http://www.w3.org/1999/xhtml"/></colgroup><thead><tr xmlns="http://www.w3.org/1999/xhtml"><th>name</th><th>amount</th><th>accumulated</th></tr></thead><tbody xmlns="http://www.w3.org/1999/xhtml"><tr xmlns="http://www.w3.org/1999/xhtml"><td>Scott</td><td>4</td><td>4</td></tr><tr xmlns="http://www.w3.org/1999/xhtml"><td>Scott</td><td>2</td><td>6</td></tr><tr xmlns="http://www.w3.org/1999/xhtml"><td>Marc</td><td>3</td><td>3</td></tr><tr xmlns="http://www.w3.org/1999/xhtml"><td>Marc</td><td>4</td><td>7</td></tr><tr xmlns="http://www.w3.org/1999/xhtml"><td>Marc</td><td>5</td><td>12</td></tr></tbody></table></div></div></div><div xmlns="http://www.w3.org/1999/xhtml" class="navfooter"><hr/><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch09s03.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch09.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch10.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Operators </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 10. Using a Plugin</td></tr></table></div></body></html>
