String Processing

advertisement
String Processing
• Operations on numeric edited fields
i.e., view the PIC as a string of characters
– Reference modification
• read/write substrings
– INSPECT
• replacing, converting, and counting substrings
– STRING
• join substrings
– UNSTRING
• split substrings
1
Reference Modification (1)
•
•
Access substrings within a field
Format
– identifier (offset from left : length)
MOVE “*” TO WORK-FIELD (20:1).
MOVE SSN (4:2) TO USER-PASSWD (3:2).
•
Omit length to default to the string end
MOVE ZIP-CODE TO ADDRESS (15:).
•
Table subscript comes before reference modification
TABLE-ITEM (20) (5:1)
2
Reference Modification (2)
05 TELEPHONE-NUM
PIC X(10) VALUE ‘6145551212’
01 EDITED-PHONE-NUM.
05 FILLER
PIC X VALUE ‘(‘.
05 AREA-CODE
PIC X(3).
05 FILLER
PIC X VALUE ‘)’.
05 FILLER
PIC X VALUE ‘ ‘.
05 EXCHANGE
PIC X(3).
05 FILLER
PIC X VALUE ‘-’.
05 DIGITS
PIC X(4).
…
MOVE TELEPHONE-NUM (1:3) TO AREA-CODE.
MOVE TELEPHONE-NUM (4:3) TO EXCHANGE.
MOVE TELEPHONE-NUM (7:4) TO DIGITS.
? EDITED-PHONE-NUM = “(614) 555-1212”
3
Utility String Operations
• Invoke with the statement FUNCTION
–
–
–
–
–
–
NUMVAL – string (‘+’, ‘-’) to number
NUMVAL-C – string (‘$’, ‘,’) to number
LOWER-CASE
UPPER-CASE
LENGTH
REVERSE
4
INSPECT Operation (1)
• REPLACING clause
INSPECT ident-1 REPLACING
{ALL/LEADING/FIRST ident-2/lit-1
BY ident-3/lit-2 {BEFORE/AFTER ident-4/lit-3}}
– ALL - replace all occurrences
– LEADING – replace occurrences from start
– FIRST – replace just first occurrence
5
INSPECT Operation (2)
01 SSNUM
PIC X(9)
01 SSN-OUT
PIC XXXBXXBXXXX
…
MOVE SSNUM TO SSN-OUT.
INSPECT SSN-OUT REPLACING ALL ‘ ‘ BY ‘-’.
-----------------------------------------------------------------------------------------INSPECT WORK-FIELD REPLACING
ALL “A” BY “a” BEFORE “.”
FIRST “B” BY “b”
ALL “C” BY “c” AFTER “.”.
WORK-FIELD : “BACK.BACK” ? “baCK.BAcK”
6
INSPECT Operation (3)
•
CONVERTING clause
INSPECT ident-1 CONVERTING
ident-2/lit-1 TO ident-3/lit-2 {BEFORE/AFTER ident-4/lit-3}
INSPECT WORK-FIELD CONVERTING
“*” TO “0”
WORK-FIELD : “**123.56*” ? “00123.560”
•
TALLYING clause
INSPECT ident-1 TALLYING
ident-2 FOR { CHARACTERS/ALL/LEADING ident-3/lit-1
{BEFORE/AFTER ident-4/lit-2}}
INSPECT WORK-FIELD TALLYING
COUNT-1 FOR ALL “*” BEFORE “.”
COUNT-2 FOR CHARACTERS AFTER “.”.
WORK-FIELD : “***12.**” ? COUNT-1 = 3, COUNT-2 = 2
7
STRING
• Concatenate substrings into a string
STRING {ident-1/lit-1 DELIMITED BY
ident-2/lit-2/SIZE} INTO ident-3
WITH POINTER ident-4.
8
05 NAME-IN-PIECES.
10 LAST-NAME
10 FIRST-NAME
10 MIDDLE-INITIAL
PIC X(16).
PIC X(10).
PIC X.
05 ENTIRE-NAME
PIC X(29).
Before Execution
LAST-NAME
SM I TH
MIDDLE-INITIAL
H
FIRST-NAME
JOHN
ENTIRE-NAME
9
MOVE SPACES TO ENTIRE-NAME.
STRING FIRST-NAME DELIMITED BY SPACE
‘ ‘ DELIMITED BY SIZE
MIDDLE-INITIAL DELIMITED BY SPACE
‘ ‘ DELIMITED BY SIZE
LAST-NAME DELIMITED BY SPACE
INTO ENTIRE-NAME
Execution Steps
(1) ENTIRE-NAME
JOHN
(2) ENTIRE-NAME
J O H N (b)
(3) ENTIRE-NAME
J O H N (b) H
(4) ENTIRE-NAME
J O H N (b) H (b)
(5) ENTIRE-NAME
J O H N (b) H (b) S M I T H
10
UNSTRING
• Split a string into substrings
UNSTRING ident-1 { DELIMITED BY
{ALL} ident-2/lit-1} INTO ident-list
WITH POINTER ident-3
11
05 NAME-IN-PIECES.
10 LAST-NAME
10 FIRST-NAME
10 MIDDLE-INITIAL
PIC X(16).
PIC X(10).
PIC X.
05 ENTIRE-NAME
PIC X(31).
MOVE SPACES TO NAME-IN-PIECES.
UNSTRING ENTIRE-NAME DELIMITED BY ‘ ‘
INTO FIRST-NAME MIDDLE-INITIAL LAST-NAME.
Before Execution
LAST-NAME
MIDDLE-INITIAL
FIRST-NAME
ENTIRE-NAME
J O H N (b) H (b) S M I T H
12
(1) ENTIRE-NAME
J O H N (b) H (b) S M I T H
(1) ENTIRE-NAME
J O H N (b) H (b) S M I T H
JOHN
FIRST-NAME
H
SM I TH
MIDDLE-INITIAL
LAST-NAME
13
Subprograms
• What is a subprogram?
– cobol code in different file(s)
– pre-compiled
– call from within your program
• Why?
– fast linking with your program
– shared by many applications
– basis for libraries of useful code
14
Subprogram Format
• A subprogram is the same as a program except :
– extra program division called LINKAGE SECTION.
– PROCEDURE DIVISION has a USING clause
– EXIT PROGRAM instead of STOP RUN.
…
LINKAGE SECTION.
… Data definitions for fields coming from the calling program …
…
PROCEDURE DIVISON USING ident-1 …
…
EXIT PROGRAM.
15
Calling the Subprogram
CALL “subprogram-name” USING ident-1 …
• In the subprogram, the fields in the LINKAGE
SECTION must match the list in the USING
clause of the PROCEDURE DIVISION
• The lists in both USING clauses of CALL and the
PROCEDURE DIVISON must match
• See the book for an example
16
Download