Reference modification - IBM Mainframe

Reference modification is a syntax for referencing a portion (substring) of a data item. The reference defines a temporary, unique data item. It may be used anywhere in the Procedure Division. You simply specify the leftmost character position within the data item where you want the substring to start, and the length of the substring. The length is optional and if you omit the length, it will automatically extend to the end of the subject data item. Both the leftmost character position and the length value can be written as integer literals, data items, or arithmetic expressions.

The leftmost character position must be at least 1 and cannot be greater than the length of the referenced data item. The length must be at least 1. The sum of the leftmost character position and the length must not exceed the total length of the data item.

The SSRANGE compiler and the SSRANGE run-time option will detect out-of-range reference modification conditions and flag violations with a run-time message.

Assume that you want to retrieve the current time from the system and display its value in an expanded format. You can retrieve the current time value from the system with the ACCEPT statement, which returns the hours, minutes, seconds, and hundredths of seconds in this format: HHMMSSSS

However, you might prefer to view the current time in this format: HH:MM:SS

Without reference modification, the following data items would have to be defined:

Reference modification

Reference modification

The following code would retrieve the TIME value, convert it to its expanded format, and display the new value:

retrieve the TIME value

With reference modification, you need not provide names for the sub-fields that describe the TIME elements. The only data definition that is needed is:

01 REFMOD-TIME-ITEM PICX(8).

The code to retrieve and expand the time value would appear as follows:

REFMOD-TIME-ITEM

REFMOD-TIME-ITEM

  • The reference, REFMOD-TIME-ITEM (1:2), causes a reference beginning at character position 1, for a length of 2, thus retrieving the portion of the time value that corresponds to the number of hours.
  • The reference, REFMOD-TIME-ITEM (3:2), causes a reference beginning at character position 3, for a length of 2, thus retrieving the portion of the time value that corresponds to the number of minutes.
  • The reference, REFMOD-TIME-ITEM (5:2), causes a reference beginning at character position 5, for a length of 2, thus retrieving the portion of the time value that corresponds to the number of seconds.

Reference Modification with Variable-Length Tables

Assume that variable-length tables are being used to contain names:

Reference Modification with Variable-Length Tables

Reference Modification with Variable-Length Tables

The OCCURS DEPENDING ON object of the WS-NAME-PORTION table is set to 6 so that only the first 6 occurrences of the table are referenced, even though the entire 11 bytes of WS-NAME-PORTION is filled in. 06ALEXIS. LEON

Suppose you want to change the value in the item WS-NAME-PORTION without changing the portion of the item that is defined beyond the currently defined length, you might try the following code:

MOVE WS-NEW-NAME-GROUP TO WS-NAME-GROUP

Where the content of WS-NEW-NAME-GROUP is (where 'B' represents spaces): 04EMILBBBBBBB

However, according to the rules for the MOVE statement, the entire content of the receiving field WS-NAME-GROUP will get replaced. The problem of changing the entire content of WS-NAME-GROUP can be avoided by using reference modification in the MOVE statement:

MOVE WS-NEW-NAME-GROUP TO WS-NAME-GROUP ( 1 : LENGTH OF WS-NAME-GROUP )

By specifying the reference modification using LENGTH OF special register, the reference to WS-NAME-GROUP is automatically made to the end of the item (determined by its actual length). The new value of WS-NAME-GROUP will be: 04EMILBB .LEON

Reference Modification Using Data-Names

So far, all of the reference modification examples have shown simple numeric literals as the reference modification starting position and length values. However, these values can also be data items or arithmetic expressions. Suppose that a field contains some characters, right justified, and you want to move the characters to another field, but justified left instead of right. Using reference modification and an INSPECT statement, you could do just that.

The program would have the following data:

Reference Modification Using Data-Names

Reference Modification Using Data-Names

The program would then count the number of leading spaces and, using arithmetic expressions in a reference modification expression, move the right-justified characters into another field, left-justified:

Reference Modification Using Data-Names

The MOVE statement transfers characters from WS-RIGHT, beginning at the position computed in WS-COUNT + 1, for a length that is computed in LENGTH OF WS-RIGHT - WS-COUNT. into the field WS-LEFT. In the previous example, both the leftmost character position and the length were specified in the reference modifier for WS-RIGHT to illustrate the use of arithmetic expressions as reference modifiers. However, remember that specifying the length is optional. If it is omitted, the substring created will automatically extend to the end of the item. Thus, coding:

MOVE statement transfers characters from WS-RIGHT

Omitting the length, when possible, is recommended as a simpler, less error-prone coding technique.

Reference Modification with Subscripting

Both reference modification and subscripting can be specified for. a table element in the same statement. For example, if you define a table like this:

Reference Modification with Subscripting

You can change both the third and fourth bytes in the first element of WS-ITEM so that both bytes contain the value "?". This could be performed with the following MOVE statement:

Reference Modification with Subscripting

This will move the value "??" into table element number 1, beginning at character position 3, for a length of 2. So the WS-TABLE which contained 3 rows of ABCDEFGHIJ before the move, will contain the rows AB??EFGHIJ, ABCDEFGHIJ and ABCDEFGHIJ after the move as shown below:

WS-TABLE

WS-TABLE


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

IBM Mainframe Topics