contentsconceptssyntaxlibrariessampleserrorsindex
Full text search
Arrays
     

Most programming languages allow programmers to store and manipulate values in arrays, associative arrays, queues, and stacks. Instead, OmniMark provides a data container called a "shelf" which can be used to accomplish all of the tasks normally carried out by these various structures in other programming languages. Like arrays, shelves can be indexed by numeric values that reflect the position of the elements they contain or, like associative arrays, these elements can be given names (keys) and then indexed by those keys.

A shelf is a data structure that is used to store one or more values of a certain type. Stream shelves can be used to store one or more string values, counter shelves to store one or more numeric values, and switch shelves to store one or more Boolean values.

A global stream shelf declaration that creates a shelf of variable size named quotations would look like this:

  global stream quotations variable

A local counter shelf declaration that creates a counter shelf named "count1" that can contain three (and only three) numeric values would look like this:

  local counter count1 size 3

If you want to create a shelf with initial values that are different from these defaults, you can do this by adding an initial keyword to the declaration, followed by the values you want on the shelf being enclosed in curly braces. For example:

  global counter count2 size 4 initial {1, 2, 3, 4}

This declaration creates a global counter shelf named "count2" that can hold four values with initial values of "1", "2", "3", and "4". You could also create a variable-sized shelf that contains a number of initial values, as follows:

  global counter count3 variable initial {1, 2, 3, 4}

The only difference between these two shelves (other than their names) is that while "count2" is a fixed-size shelf holding four values, "count3" begins with four values and can be expanded or contracted to hold as many as required. If you're not sure how many values you will need to store on a shelf, it's best to declare it with a variable size.

Additionally, shelves of a particular size can be created without having to assign initial values to the shelf items. This is accomplished by using the initial-size keyword:

  global counter count4 variable initial-size 4

This shelf declaration creates a counter named "count4" that starts with space for four items and can be expanded or contracted as required.

To store the string "Now is the winter of our discontent" in the stream shelf "quotations", you would use the following action:

  set quotation to "Now is the winter of our discontent"

This begs the question "where on the shelf was this value stored?" Unless you explicitly specify which item on a shelf you want a value stored in, a value will be stored in the current item. A shelf is basically an ordered list of items ranging from 1 to n. The default behavior of a shelf is that all new items are added after n. If you use set to store a different value on the shelf without specifying a different item, it will simply replace the n value on the shelf.

To change this default behavior, you can use either of two shelf indexing methods. The first index is based upon the position number of a value on a shelf. For example, the following code sets a value in the third position of the "quotation" shelf:

  set quotation item 3 to "Words, words, words."

The second index is based upon names or "keys" that are assigned to each value on a shelf. To set the key of the current item on a shelf, you would use the following code:

  set key of quotation to "Richard iii"

To set the key of a particular item on a shelf, you would use the same code, but adding a position index:

  set key of quotation item 3 to "Hamlet"

Using the key index of a shelf is very like using the position index, except instead of using the item keyword, you use the key keyword:

  set quotation key "Hamlet" to "To be or not to be?"

It is possible to set a key on a shelf item when it is created. This is accomplished by setting the key in the same action in which the new item is created. For example, to create a new item on the "quotes" shelf that has a value of "Alas, poor Yorick." with the key "Hamlet", you would use the action:

  set new quotes key "Hamlet" to "Alas, poor Yorick."

Up to this point, every time we have created a new item on a shelf, it has been added at the lastmost position of the shelf. If you want to create a new item somewhere else on a shelf, this can be accomplished by using the before or after keywords in the same action used to create the new item. For example, if you want to create a new item that will exist immediately before the second item on a shelf, you would use the following action:

  set new quotes before item 2 to "A horse!"

This would create a new item containing the value "A horse!" between the first and second items on the "quotes" shelf. Since the item numbers are based on shelf position, this new item would become item 2, and the item that was number 2 would become number 3. If the values had assigned keys, of course, these keys would not change.

If you wanted to create a new item on a shelf just after an item that had the key "MacBeth", you would use the action:

  set new quotes after key "MacBeth" to "A horse!"

To illustrate all of this, the following program creates a global stream shelf, and sets the first item on that shelf to a value. Following that, the program gives that first item a key. Then three other items are created: one at the "default" end of the shelf, another before the second item on the shelf, and the third after a value with a set key.

  global stream quotes variable

  process
     set quotes to "To be or not to be?"
     set key of quotes item 1 to "Hamlet"
     set new quotes key "MacBeth" to "Is this a dagger?"
     set new quotes key "Richard iii" before item 2 to "Now is the winter of out discontent."
     set new quotes key "Romeo" after key "Richard iii" to "Hark, what light through yonder window breaks?"

     repeat over quotes
        output key of quotes || " - "
        output "%g(quotes)%n"
     again

This program will have the following 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?

       
----

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

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

Copyright © OmniMark Technologies Corporation, 1988-1999.