swirl Guide to OmniMark 5   OmniMark home
docs home 
IndexConceptsTasksSyntaxLibrariesOMX VariablesErrors
    Related Syntax  

A shelf is an ordered collection of values of a particular type. You can access items on a shelf either by item number or by key value. A key is a text value. Each key on a shelf must be unique.

Creating a shelf

A shelf can be of fixed or variable size. In fact, an ordinary variable declaration creates a shelf with a fixed size of 1 item:

  local integer word-count

You can create shelves of a fixed size by specifying the size in the declaration:

  local stream days-of-the-week size 7

You can create a variable sized shelf, by specifying variable in the declaration.

  local integer word-count variable

Addressing items on a shelf

If a shelf has only one item, you can address that item by the name of the shelf alone:

  output template-file-name

If there are multiple items on the shelf you can address each item by specifying the item number in square brackets:

  set template-file-name[5] to "foo.txt"

The item value can be given either as an integer literal or as an integer expression:

  open template-file
   as file template-file-name[last-template-number + 1]

To address an item by its key, enclose enclose the key string in curly braces:

  set template-file-name{"home-page"} to "home.txt"

The key value can be given either as a literal string or as a string expression:

  output grid-cell{"d" % row || "-" || "d" % col}

You can address the key of a shelf item using the expression key of:

  output key of shopping-list[2]
  set key of template-filename[7] to "order-page"

You can retrieve the item number of an item using the expression item of, but you cannot change an item number:

  output item of template-filename{"order-page"}

Adding and removing items on a variable shelf

You can add items to a variable shelf with the new and set new keywords. The following line creates a new item on the shelf word-count with the key "Hamlet" and the value 33452 (just guessing!):

  set new word-count{"Hamlet"} to 33452

You don't have to give every item a key. If all you need is a simple array, you can create shelf items without specifying keys:

  local stream shopping-list variable
  set new shopping-list to "lettuce"

Alternatively, you can create a shelf which has keys but no values. This is useful if you want to create an array in which every item is guaranteed to be unique. You can test a key for uniqueness before adding it to a shelf:

  local stream shopping-list variable
  do when shopping-list hasnt key "lettuce"
     new shopping-list{"lettuce"}

By default, new items are added to the end of the shelf. You can specify a different location using the keywords before and after:

  local stream shopping-list variable
  set new shopping-list{"lettuce"} after {"cabbage"}

You can remove an item from a shelf using the remove keyword:

  remove word-count{"Hamlet"}
  remove shopping-list[7]

Note that adding or removing items from a shelf causes the item numbers of all the items above the insertion point to change. Item numbers are not permanently attached to items. They are indexes into the shelf by position, not properties of the items. Keys, on the other hand, are properties of each shelf item, so the relationship between an item and its key is maintained when items are added or removed from a shelf.

Repeating over a shelf

You can perform an operation on each item on a shelf in turn using the repeat over loop:

  repeat over shopping-list
     output shopping-list || "%n"

Within a repeat-over block, you refer to the current item using the base-name of the shelf, without an indexer. You can retrieve the current item number using #item. You can test for the first and last items using #first and #last.

The following program creates a shelf, using various methods to position items on the shelf:

     local stream quotes variable
     set new quotes{"Hamlet"}
      to "To be or not to be?"
     set new quotes{"Macbeth"}
      to "Is this a dagger?"
     set new quotes{"Richard iii"} before [2]
      to "A horse!"
     set new quotes{"Romeo"}
      after {"Richard iii"}
      to "Hark, what light through yonder window breaks?"

     repeat over quotes
        output key of quotes
            || " - "
            || quotes || "%n"

This program will output:

  Hamlet - To be or not to be?
  Richard III - A horse!
  Romeo - Hark, what light through yonder window breaks?
  Macbeth - Is this a dagger?

The current item

Every shelf has a current item. If you refer to the shelf without using an indexer, the reference is to the current item.

The default current item is the last item. You can change the current item in one of three ways:

  1. The current item is the next item in succession each time through a repeat over loop.
  2. You can designate the current item with a using statement.
  3. You can establish the current item for a shelf passed (as modifiable or read-only to a function call.

There are two cases where a shelf has no current item:

  1. when it has zero items
  2. when the current item established by using has been removed

Operations on shelves

You can perform a number of operations on shelves:

Deprecated indexing methods

Earlier versions of OmniMark used a different indexing method. This method is deprecated but is supported for compatibility with programs written for older versions.

In this indexing method, item indexes were indicated with the keyword item and key indexes were indicated with the keyword key. The symbol @ is a synonym for item and the symbol ^ is a synonym for key.

Item is a deprecated synonym for [...].

@ is a deprecated synonym for [...]. The keywords key and ^ are deprecated synonyms for the {...} operator.

      Related Syntax
   global, local
   has key
   key of
   number of
   remove key of
   repeat over, #item, #last, #first


Generated: August 11, 2000 at 3:06:29 pm
If you have any comments about this section of the documentation, send email to docerrors@omnimark.com

Copyright © OmniMark Technologies Corporation, 1988-2000.