HP 3000 Manuals

Defining and Handling Arrays [ HP Transact Reference Manual ] MPE/iX 5.0 Documentation


HP Transact Reference Manual

Defining and Handling Arrays 

Simple Arrays 

The compound item is the simplest form of a Transact array.  For example:

     DEFINE(ITEM) INVOICE-NO 100 X(10);

Defines 100 occurrences of an X(10) type item called INVOICE-NO.
Graphically, this array can be portrayed as follows:

     -------------------------Occurrences of INVOICE-NO------------------------

        > <   54   > <   55   > <   56   > <   57   > <   58   > <   59   > <
     ----|----------|----------|----------|----------|----------|----------|---
     789N|ABC123456D|STX432849D|URE849328D|NVM215425N|WAS950789N|YUR956789N|BB
     A
     ----|----------|----------|----------|----------|----------|----------|---

The value in INVOICE-NO(56) is URE849328D, the value in INVOICE-NO(59) is
YUR956789N, etc.

This array can be manipulated in the following manner:

     MOVE(INVOICE-NO(4)) = (INVOICE-NO(80));

Assigns the eightieth occurrence of the array to the fourth occurrence of
the array.

     DISPLAY INVOICE-NO((INDEX));

Displays the occurrence of the array equal to the current value of the
data item INDEX.

     DISPLAY INVOICE-NO;

Displays the entire compound item.  Transact treats references to
unsubscripted arrays as ordinary compound items.  There is one exception
to this rule.  When the IF statement is used with non-subscripted
compound items, only the first element of the compound item is used for
the comparison.

The following example shows how the IF statement only compares the first
element in an array:

     SYSTEM T320;

     DEFINE(ITEM) INVOICE-NO-A 100 X(10):
                  INVOICE-NO-B 100 X(10);

     LIST         INVOICE-NO-A:
                  INVOICE-NO-B;
     MOVE (INVOICE-NO-A(1)) = "G200500001";
     MOVE (INVOICE-NO-A(2)) = "9999999999";
     MOVE (INVOICE-NO-B(1)) = "G200500001";
     DISPLAY INVOICE-NO-A;
     DISPLAY INVOICE-NO-B;

      IF (INVOICE-NO-A) = (INVOICE-NO-B) THEN
        DISPLAY "THE FIRST ELEMENTS ARE EQUAL"
     ELSE
        DISPLAY "THE FIRST ELEMENTS ARE NOT EQUAL";

     MOVE (INVOICE-NO-B) = (INVOICE-NO-A);

     DISPLAY INVOICE-N0-B;

     EXIT;

The following output is generated by the above example program:

     INVOICE-NO-A:
      G200500001 9999999999

     INVOICE-NO-B:
      G200500001

     THE FIRST ELEMENTS ARE EQUAL

     INVOICE-NO-B
      G200500001 9999999999

The example above shows the difference between the IF and MOVE statements
with non-subscripted compound items.  The IF statement results in the
invoice data items being equal since Transact only compares the value of
the first element of the array when this statement is used.

The last MOVE statement results in all elements of INVOICE-NO-A being
copied to the corresponding elements of INVOICE-N0-B.

Transact does not allow you to manipulate arrays with an occurrence count
of 1.  The following example causes a run-time error message, "Cannot
subscript a non-array item," to appear.

     Move (one(1)) = "A";

An array with an occurrence count of 1 is considered a non-array item by
Transact and therefore cannot be used.

Complex Arrays 

Complex arrays have one or more child items associated with each
occurrence of the compound parent item.

The child items may be defined as simple items or compound items.  The
child items can be parent items with child items defined as well.  Each
child item definition that is a compound item adds a level to the array
definition.  Each level is a redefinition of its parent, thus providing
more detailed definition of the array.

Child Identified as Simple Items.   

The following is an example of a complex array definition whose array
child items are defined as simple items.

     DEFINE(ITEM) INVOICE-NO 100  X(10):
                     INVOICE-PFX  X(3) = INVOICE-NO(1):
                     INVOICE-SFX  9(6) = INVOICE-NO(4);

Defines 100 occurrences of an X(10) type item called INVOICE-NO. In
addition, it defines two child items, INVOICE-PFX and INVOICE-SFX, for
each element of the INVOICE-NO array.

Each of the child items can be subscripted even though they are not
themselves compound items.  When subscripted, the value of the subscript
applies to the compound parent item of the child item referenced.  This
structure is very similar to a Pascal array of records structure.

Graphically, this structure can be portrayed in the following manner:

     -----------------------Occurrences of INVOICE-NO -------------------------
        > <   54   > <   55   > <   56   > <   57   > <   58   > <   59   > <
     ----|----------|----------|---------|----------|----------|----------|---
     789N|ABC123456D|STX432849D|URE849328|NVM215425N|WAS950789N|YUR956789N|BBA
     ----|----------|----------|---------|----------|----------|----------|---

The value in INVOICE-NO(55) is STX432849D and the values in
INVOICE-PFX(55) and INVOICE-SFX(55) are STX and 432849, respectively.

For subscripting to work correctly, the total storage length of all child
items defined in such a structure must be less than or equal to the
storage length for each occurrence of the parent.  Child elements must
not span across occurrences of the parent.  The following structures, for
example, would be incorrect:

     DEFINE(ITEM) INVOICE-NO 1000  X(1):
                     INVOICE-PFX   X(3) = INVOICE-NO(1):
                     INVOICE-SFX   9(7) = INVOICE-NO(4);

This structure defines 1000 occurrences of an X(1) type item called
INVOICE-NO. The child items, totaling 10 bytes in storage length, cannot
fit within a parent that is only 1 byte long.

     DEFINE(ITEM) INVOICE-NO 100  X(10):
                     INVOICE-PFX  X(4) = INVOICE-NO(1):
                     INVOICE-SFX  9(7) = INVOICE-NO(5);

This structure is incorrect because the child items total 11 bytes and
thus cannot be defined within a parent item that is 10 bytes long.
However, this structure

     DEFINE(ITEM) INVOICE-NO 100  X(10):
                     INVOICE-PFX  X(4) = INVOICE-NO(1):
                     INVOICE-SFX  9(7) = INVOICE-NO(4);

is correct because, although the total storage lengths of child items is
greater than the storage length of the parent, the last byte of
INVOICE-PFX and the first byte of INVOICE-SFX overlap, thereby requiring
only 10 bytes of total storage length.

Child Items Defined as Compound Items.   

The following example illustrates an array definition whose child items
are defined as compound items.  For simplicity, this example assumes that
all months are 28 days long.  The item YEAR will hold one character for
each day of the year.  YEAR is not defined as an array.  However, the
definition of its child items MONTH, WEEK, and DAY redefine it into
arrays for easier data manipulation.  Each of the child items are
compound items; MONTH and WEEK are also parent items.  This definition
contains three levels.

     DEFINE(ITEM) YEAR X(336):
                      MONTH 12 X(28) = YEAR(1):
                           WEEK 4 X(7) = MONTH(1):
                               DAY 7 X(1) = WEEK(1);

The following example displays a specific day of the year:

     DISPLAY DAY(2,1,5);

This verb statement displays the fifth day of the first week in the
second month.  Since YEAR is not a compound item, the first subscript (2)
refers to the highest level compound item, MONTH. The next subscript (1)
refers to the WEEK which is the next highest level compound item.
Finally, the last subscript (5), refers to the fifth occurrence of the
compound item, DAY.

The next example shows what happens when succeeding subscripts are
omitted:

     DISPLAY DAY(2);

Since the omitted subscripts default to 1, this is equivalent to (2,1,1)
that displays the first day of the first week in the second month.

When the number of subscripts exceeds the number of dimensions specified
by the DEFINE statement, an error occurs.  For example:

     DISPLAY DAY(2,1,5,3);

Obviously, there are not enough parent levels to justify four subscripts.
This example will result in an error and the message, "Too many
subscripts for item," is displayed.

If the subscripts are larger than the specified range an error occurs.
For example:

     DISPLAY DAY(999);

This example results in an error and the message, "Array subscript is out
of range", because 999 is beyond the 12 element definition.

Under some conditions, you may want to reference globally all of the
elements in the array.  For example:

     MOVE (YEAR) = " ";

Since there are no subscripts specified, this will set all 336 elements
to blanks by moving a space to the first element of the year and filling
the remaining 335 elements with blanks.  This differs from the following
example:

     MOVE (DAY) = "1";

which sets the first character of DAY to a 1 as requested, then pads the
remaining six characters with blanks.  In the next example, the subscript
is specified:

     MOVE (DAY(1)) = "X";

which results in the element DAY (1,1,1) being set to an X. Since the
length of the source and destination are the same, no filling is done.


NOTE Since Transact originally supported only single-level array subscripting, the LET OFFSET construct was used to reference arrays with multiple levels. This is no longer necessary. We strongly recommend that you address array items by using subscripts. However, there may still be programs that manipulate arrays in this manner. If this is the case, you should be careful not to combine the use of the LET OFFSET verb with a subscripted item. Doing so may cause the program to update the data register in areas outside the limits of the item referenced and could lead to unpredictable results. Since this was previously the only way to simulate arrays with multiple levels, no error message is generated.
Special Considerations If any level definition in an array structure has an occurrence of 1, it is ignored by Transact as part of the array definition. For example, DEFINE(ITEM) INVOICE-DATA X(1000): INVOICE-NO 100 X(10) = INVOICE-DATA(1); is a single level array of 100 invoice numbers. Any subscript applied to INVOICE-NO is treated as a subscript to the INVOICE-NO array. Likewise, DEFINE(ITEM) INVOICE-DATA 100 X(10): INVOICE-NO X(10) = INVOICE-DATA(1); is also a single-level array of 100 invoice numbers. By the same token, DEFINE(ITEM) INVOICE-DATA X(1000): INVOICE-NO 100 X(10) = INVOICE-DATA(1) : INVOICE-PFX X(3) = INVOICE-NO(1) : INVOICE-SFX 9(6) = INVOICE-NO(4) ; is also a single-level array because INVOICE-DATA is not a compound item. The child items of INVOICE-NO are simple items and do not add a level to the array structure.


MPE/iX 5.0 Documentation