IBM - VisualAge Cobol for Windows Programming Guide
634 pág.

IBM - VisualAge Cobol for Windows Programming Guide


DisciplinaCobol25 materiais167 seguidores
Pré-visualização50 páginas
construct and manipulate null-terminated strings (passed to or from a C
program, for example), by various mechanisms:
v Use null-terminated literal constants (Z\u201c. . . \u201d).
v Use an INSPECT statement to count the number of characters in a null-terminated
string:
MOVE 0 TO char-count
INSPECT source-field TALLYING char-count
FOR CHARACTERS
BEFORE X\u201c00\u201d
v Use an UNSTRING statement to move characters in a null-terminated string to a
target field, and get the character count:
WORKING-STORAGE SECTION.
01 source-field PIC X(1001).
01 char-count COMP-5 PIC 9(4).
01 target-area.
02 individual-char OCCURS 1 TO 1000 TIMES DEPENDING ON char-count
PIC X.
. . .
PROCEDURE DIVISION.
. . .
UNSTRING source-field DELIMITED BY X\u201c00\u201d
INTO target-area
COUNT IN char-count
Chapter 6. Handling strings 83
ON OVERFLOW
DISPLAY \u201csource not null terminated or target too short\u201d
. . .
END-UNSTRING
v Use a SEARCH statement to locate trailing null or space characters. Define the
string being examined as a table of single characters.
v Check each character in a field in a loop (PERFORM). You can examine each
character in the field by using a reference modifier such as source-field (I:1).
\u201cExample: null-terminated strings\u201d
RELATED REFERENCES
Nonnumeric literals (IBM COBOL Language Reference)
Example: null-terminated strings
The following example shows several ways you can manipulate null-terminated
strings:
01 L pic X(20) value z'ab'.
01 M pic X(20) value z'cd'.
01 N pic X(20).
01 N-Length pic 99 value zero.
01 Y pic X(13) value 'Hello, World!'.
. . .
* Display null-terminated string
Inspect N tallying N-length
for characters before initial x'00'
Display 'N: ' N(1:N-Length) ' Length: ' N-Length
. . .
* Move null-terminated string to alphanumeric, strip null
Unstring N delimited by X'00' into X
. . .
* Create null-terminated string
String Y delimited by size
X'00' delimited by size
into N.
. . .
* Concatenate two null-terminated strings to produce another
String L delimited by x'00'
M delimited by x'00'
X'00' delimited by size
into N.
Referring to substrings of data items
Refer to a substring of a character-string data item (including EBCDIC data items)
with reference modifiers. Intrinsic functions that return character-string values are
also considered alphanumeric data items, and can include a reference modifier.
The following example shows how to use a reference modifier to refer to a
substring of a data item:
Move Customer-Record(1:20) to Orig-Customer-Name
As this example shows, you code two values separated by a colon, in parentheses,
immediately following the data item:
v Ordinal position (from the left) of the character that you want the substring to
start with
v Length of the desired substring
84 Programming Guide
The length is optional. If you omit the length, the substring extends to the end of
the item. Omit the length when possible as a simpler and less error-prone coding
technique.
You can code either of the two values as a variable or as an arithmetic expression.
Because numeric function identifiers can be used anywhere arithmetic expressions
are allowed, you can use them in the reference modifier as the leftmost character
position or as the length.
You can also refer to substrings of table entries, including variable-length entries.
Both numbers in the reference modifier must have a value of at least 1. Their sum
should not exceed the total length of the data item by more than 1 so that you do
not reference beyond the end of the desired substring.
If the leftmost character position or the length value is a fixed-point noninteger,
truncation occurs to create an integer. If either is a floating-point noninteger,
rounding occurs to create an integer.
The following options detect out-of-range reference modifiers, and flag violations
with a run-time message:
v SSRANGE compiler option
v CHECK run-time option
RELATED CONCEPTS
\u201cReference modifiers\u201d
RELATED TASKS
\u201cReferring to an item in a table\u201d on page 53
RELATED REFERENCES
\u201cSSRANGE\u201d on page 189
Reference modification (IBM COBOL Language Reference)
Function definitions (IBM COBOL Language Reference)
Reference modifiers
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 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 modifiers, you would have to define data items for both formats.
You would also have to write code to convert from one format to the other.
With reference modifiers, you do not need to provide names for the subfields that
describe the TIME elements. The only data definition you need is for the time as
returned by the system. For example:
01 REFMOD-TIME-ITEM PIC X(8).
Chapter 6. Handling strings 85
The following code retrieves and expands the time value:
ACCEPT REFMOD-TIME-ITEM FROM TIME.
DISPLAY \u201cCURRENT TIME IS: \u201d
* Retrieve the portion of the time value that corresponds to
* the number of hours:
REFMOD-TIME-ITEM (1:2)
\u201c:\u201d
* Retrieve the portion of the time value that corresponds to
* the number of minutes:
REFMOD-TIME-ITEM (3:2)
\u201c:\u201d
* Retrieve the portion of the time value that corresponds to
* the number of seconds:
REFMOD-TIME-ITEM (5:2)
\u201cExample: arithmetic expressions as reference modifiers\u201d
\u201cExample: intrinsic functions as reference modifiers\u201d
RELATED TASKS
\u201cReferring to substrings of data items\u201d on page 84
RELATED REFERENCES
Reference modification (IBM COBOL Language Reference)
Example: arithmetic expressions as reference modifiers
Suppose that a field contains some right-justified characters, and you want to
move the characters to another field where they will be left-justified. You can do
that using reference modifiers and an INSPECT statement.
Suppose the program has the following data:
01 LEFTY PIC X(30).
01 RIGHTY PIC X(30) JUSTIFIED RIGHT.
01 I PIC 9(9) USAGE BINARY.
The program counts the number of leading spaces and, using arithmetic
expressions in a reference modifier, moves the right-justified characters into
another field, justified to the left:
MOVE SPACES TO LEFTY
MOVE ZERO TO I
INSPECT RIGHTY
TALLYING I FOR LEADING SPACE.
IF I IS LESS THAN LENGTH OF RIGHTY THEN
MOVE RIGHTY ( I + 1 : LENGTH OF RIGHTY - I ) TO LEFTY
END-IF
The MOVE statement transfers characters from RIGHTY, beginning at the position
computed as I + 1 for a length that is computed as LENGTH OF RIGHTY - I, into the
field LEFTY.
Example: intrinsic functions as reference modifiers
The following code fragment causes a substring of Customer-Record to be moved
into the variable WS-name. The substring is determined at run time.
05 WS-name Pic x(20).
05 Left-posn Pic 99.
05 I Pic 99.
. . .
Move Customer-Record(Function Min(Left-posn I):Function Length(WS-name)) to WS-name
86 Programming Guide
If you want to use a noninteger function in a position requiring an integer
function, you can use the INTEGER or INTEGER-PART function to convert the result to
an integer. For example:
Move Customer-Record(Function Integer(Function Sqrt(I)): ) to WS-name
RELATED REFERENCES
INTEGER-PART (IBM COBOL Language Reference)
INTEGER (IBM COBOL Language Reference)
Tallying and replacing data items (INSPECT)
Use the INSPECT statement to do the following:
v Fill selective portions of a data item with a value. If you specify a national data
item, you must also specify a national value.
v Replace portions of a data item with a corresponding portion of another data
item. To specify any national items or literals in the statement, you must specify
all items as national.
v Count the number of times a specific character (zero,