File I/O, Arrays, and Databases in Revolution

Rob Cozens rcozens at
Thu Mar 28 17:06:01 EST 2002

>I'm new to Revolution (I'm a long-time FORTRAN programmer), and am
>having trouble understanding how file input/output, arrays, and
>databases work in this package.

Hi Dennis,

>I've created several complex air pollution modeling programs in
>FORTRAN.  I want to use Revolution to act as the GUI interface to and
>from these programs.  As a result, Revolution must read and write files
>which the FORTRAN programs use or create.  Every line of these files are
>of mixed type: character fields, integer number fields, real number
>fields, scientific notation fields, etc.  In looking at the Revolution
>documentation, it appears that I have to somehow read or write each line
>as a string, then somehow parse each field from this string.  So how do
>you do this efficiently in Revolution?

Transcript, like other Xtalk languages is adept at string processing, 
and hides internal data formats from the user.  If you can easily 
convert integer & real variables to & from strings in FORTRAN (it's 
been so long since I used a FORTRAN compiler I've forgotten), I'd do 
it there and concentrate on string manipulation in Transcript.

Transcript offers three built-in delimiters for string parsing: word, 
line, item.

A word is delimited by one or more space characters or the return 
character, except that a quoted string counts as one word.

A line is delimited by a single return character.  It is essentially 
an item where the itemDelimiter is the return character.

An item is delimited by the last specified itemDelimiter, the default 
itemDelimiter is comma; however you can set it to any valid character 
(eg: set the itemDelimiter to numToChar(240)).

putting something into line or item x will create any missing 
lines/items between 1 and (x-1).  Putting something into word x where 
x is not 1 and word (x-1) does not exist is non sequitur: in essence, 
you can have an empty line or item, but not an empty word.

You can have items in words ("this,is,four,items") and items in lines 
("this,is,four,items"&return).  You can also have lines in items 
("This"&return&"is"&return"&"one"&return&"item"&comma). So long as 
you can convert your data to ASCII text and can identify a character 
value that can never appear in your text file as an item delimiter, 
you can set the itemDelimiter and parse away.

The preferred method of parsing the text once you have opened the 
file and read until EOF (if memory is sufficient; otherwise you'll 
have to read until return and process one line at a time) is 
something along the lines of:

put the itemDelimiter into someTempVariable -- to restore when done
set the itemDelimiter to myDelimiter
repeat for each line dataRecord of myFileData
   put item 1 of dataRecord into someStringVariable
   put item 2 of dataRecord into someInteger
   put (item 3 of it is "t") into someBoolean
end repeat
set the itemDelimiter to someTempVariable

Please note: The above syntax is MUCH more efficient than

repeat with x = 1 to the number of lines of myFileData
   put line x of myFileData into dataRecord
end repeat

>The next question is how are arrays implemented in Revolution?. I've
>searched the documentation but haven't found anything.  What I'd like to
>do is read in the FORTRAN files, putting the data into Revolution arrays
>for display or calculational analysis.  How can this be done?

You can also set up arrays of three dimensions or more using multiple 

 From my post of 7 Jan, 2002:

function getElement xCoord,yCoord,zCoord,xDelim,yDelim,zDelim,zaArray
   put the itemDelimiter into savedDelimiter
   set the itemDelimiter to zDelim
   get item zCoord of zaArray
   set the itemDelimiter to yDelim
   get item yCoord of it
   set the itemDelimiter to xDelim
   get item xCoord of it
   set the itemDelimiter to savedDelimiter
   return it
end getElement

In addition, and this is especially useful for discontinuous arrays, 
Transcript supports implicit single dimension hash tables by simply 
appending array notation to a variable name: eg: put someValue into 

>Finally, I tam interested in the database capabilities of Revolution
>(especially the Valentina engine).  I'd like to read in the data from
>the FORTRAN files and directly put them into a database so that
>information can be searched, manipulated, calculated. etc.  How can this
>be done?

Revolution includes built-in support for the Valentina engine and 
several flavors of SQL database.  In addition, it's quite easy to 
create a simple single-user database directly in transcript.  The 
demo at includes a generic db 
stack and a library of drivers in HyperCard.

>I realize these are complex questions. But the use of Revolution for
>this project hinges on how well this can be done with the package.
>Any help or guidance will be greatly appreciated.
>Dennis F. Kahlbaum
>University of Michigan

Hope this helps.

Rob Cozens
CCW, Serendipity Software Company

"And I, which was two fooles, do so grow three;
Who are a little wise, the best fooles bee."

from "The Triple Foole" by John Donne (1572-1631)

More information about the use-livecode mailing list