contentsconceptssyntaxlibrariessampleserrorsindex
Full text search
I/O and variables
    Related Syntax  

Like most languages, OmniMark has actions that assign values to variables (set) and actions that read data from and write data to files (open, put, close). Unlike most languages, OmniMark lets you perform file operations with the variable assignment actions, and change the values of stream variables with the file actions. For example, you can place a simple value in a file with the set action:

  set file "mary.txt" to "Mary had a little lamb"

And you can use open, put, and close to set the value of a variable:

  local stream Mary
  open Mary as buffer
  put Mary "Mary had a little lamb"
  close Mary

How is this magic performed? Simply, in fact, because the variable assignment syntax (set) is simply a shorthand version of the file operation syntax. That is, set Mary to "Mary had a little lamb" is equivalent to:

  open Mary as buffer
  put Mary "Mary had a little lamb"
  close Mary

The virtue of using the longer syntax is that you can put off closing the stream until later and write to it many times. This is much easier and more efficient than building up a string by a series of concatenations. So you can replace code like this:

  set Mary to "Mary had a little lamb"
  set Mary to Mary || "Its fleece was white as snow"
  set Mary to Mary || "And every where that Mary went"
  set Mary to Mary || "The lamb was sure to go"

with code like this:

  open Mary as buffer
  put Mary "Mary had a little lamb"
  put Mary "Its fleece was white as snow"
  put Mary "And every where that Mary went"
  put Mary "The lamb was sure to go"
  close Mary

You can also make your variable the temporary current output so that everything sent to output goes into that variable:

  open Mary as buffer
  using output as Mary
  do
     output "Mary had a little lamb"
     output "Its fleece was white as snow"
     output "And everywhere that Mary went"
     output "The lamb was sure to go"
  done

This is an enormously powerful feature of OmniMark. It enables you to choose the type of data assignment mechanism appropriate to the scale of operation you want to perform. You can use set for any kind of small scale assignment, whether to a file or a variable, without any of the bother of opening files or buffers. For large-scale operations, you can use file type operations with any file or variable and perform multiple updates without the need to specify the destination, or even worry about the kind of destination involved. Choosing the method appropriate to the scale of operation you are performing will greatly simplify your code.

A stream must be closed before it can be read or output:

  open Mary as buffer
  put Mary "Mary had a little lamb"
  close Mary
  output Mary

You can use the action reopen to reopen a closed stream with its original content. However, if you use open to open the stream again, the existing content is lost:

  open Mary1 as buffer
  open Mary2 as buffer

  put Mary1 "Mary had a little lamb"
  put Mary2 "Mary had a little lamb"

  close Mary1
  close Mary2

  reopen Mary1
  open Mary2

  put Mary1 "Its fleece was white as snow"
  put Mary2 "Its fleece was white as snow"

This code will leave the stream Mary1 containing both lines, but Mary2 will contain only "Its fleece was white as snow".

Can you mix the two methods? Yes, but remember that "set" is a shorthand for the sequence "open...put...close" which means that a stream is always closed after a set. This means that you cannot write to it without reopening it. Also remember that a stream is always opened, not reopened, in a set, so the previous content is always lost. In practice, it is not a good idea to mix the two methods. They are really appropriate for different scales of operation. Pick the one that is appropriate to what you have to do and stick to it.

      Related Syntax
   join
   open
   output
   put
   reopen
   set
   using output as
 
----

Top [CONTENTS] [CONCEPTS] [SYNTAX] [LIBRARIES] [SAMPLES] [ERRORS] [INDEX]

Generated: April 21, 1999 at 2:00:49 pm
If you have any comments about this section of the documentation, send email to [email protected]

Copyright © OmniMark Technologies Corporation, 1988-1999.