lecture 101 string processing operations on numeric edited fields i.e., view the pic as a string of...

16
lecture 10 1 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

Upload: albert-morton

Post on 18-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 101 String Processing Operations on numeric edited fields i.e., view the PIC as a string of characters –Reference modification read/write substrings

lecture 10 1

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

Page 2: Lecture 101 String Processing Operations on numeric edited fields i.e., view the PIC as a string of characters –Reference modification read/write substrings

lecture 10 2

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)

Page 3: Lecture 101 String Processing Operations on numeric edited fields i.e., view the PIC as a string of characters –Reference modification read/write substrings

lecture 10 3

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”

Page 4: Lecture 101 String Processing Operations on numeric edited fields i.e., view the PIC as a string of characters –Reference modification read/write substrings

lecture 10 4

Utility String Operations

• Invoke with the statement FUNCTION– NUMVAL – string (‘+’, ‘-’) to number– NUMVAL-C – string (‘$’, ‘,’) to number– LOWER-CASE– UPPER-CASE– LENGTH– REVERSE

Page 5: Lecture 101 String Processing Operations on numeric edited fields i.e., view the PIC as a string of characters –Reference modification read/write substrings

lecture 10 5

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

Page 6: Lecture 101 String Processing Operations on numeric edited fields i.e., view the PIC as a string of characters –Reference modification read/write substrings

lecture 10 6

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 REPLACINGALL “A” BY “a” BEFORE “.”FIRST “B” BY “b”ALL “C” BY “c” AFTER “.”.

WORK-FIELD : “BACK.BACK” “baCK.BAcK”

Page 7: Lecture 101 String Processing Operations on numeric edited fields i.e., view the PIC as a string of characters –Reference modification read/write substrings

lecture 10 7

INSPECT Operation (3)• CONVERTING clause

INSPECT ident-1 CONVERTINGident-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 TALLYINGident-2 FOR { CHARACTERS/ALL/LEADING ident-3/lit-1

{BEFORE/AFTER ident-4/lit-2}}

INSPECT WORK-FIELD TALLYINGCOUNT-1 FOR ALL “*” BEFORE “.”COUNT-2 FOR CHARACTERS AFTER “.”.

WORK-FIELD : “***12.**” COUNT-1 = 3, COUNT-2 = 2

Page 8: Lecture 101 String Processing Operations on numeric edited fields i.e., view the PIC as a string of characters –Reference modification read/write substrings

lecture 10 8

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.

Page 9: Lecture 101 String Processing Operations on numeric edited fields i.e., view the PIC as a string of characters –Reference modification read/write substrings

lecture 10 9

05 NAME-IN-PIECES. 10 LAST-NAME PIC X(16). 10 FIRST-NAME PIC X(10). 10 MIDDLE-INITIAL PIC X.

05 ENTIRE-NAME PIC X(29).

S M I T HLAST-NAME

HMIDDLE-INITIAL

J O H NFIRST-NAME

ENTIRE-NAME

Before Execution

Page 10: Lecture 101 String Processing Operations on numeric edited fields i.e., view the PIC as a string of characters –Reference modification read/write substrings

lecture 10 10

J O H N(1) ENTIRE-NAME

J O H N (b)(2) ENTIRE-NAME

Execution Steps

J O H N (b) H(3) ENTIRE-NAME

J O H N (b) H (b)(4) ENTIRE-NAME

J O H N (b) H (b) S M I T H(5) ENTIRE-NAME

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

Page 11: Lecture 101 String Processing Operations on numeric edited fields i.e., view the PIC as a string of characters –Reference modification read/write substrings

lecture 10 11

UNSTRING

• Split a string into substrings

UNSTRING ident-1 { DELIMITED BY {ALL} ident-2/lit-1} INTO ident-list

WITH POINTER ident-3

Page 12: Lecture 101 String Processing Operations on numeric edited fields i.e., view the PIC as a string of characters –Reference modification read/write substrings

lecture 10 12

05 NAME-IN-PIECES. 10 LAST-NAME PIC X(16). 10 FIRST-NAME PIC X(10). 10 MIDDLE-INITIAL PIC X.

05 ENTIRE-NAME PIC X(31).

LAST-NAME

MIDDLE-INITIAL

FIRST-NAME

Before Execution

J O H N (b) H (b) S M I T HENTIRE-NAME

MOVE SPACES TO NAME-IN-PIECES.UNSTRING ENTIRE-NAME DELIMITED BY ‘ ‘ INTO FIRST-NAME MIDDLE-INITIAL LAST-NAME.

Page 13: Lecture 101 String Processing Operations on numeric edited fields i.e., view the PIC as a string of characters –Reference modification read/write substrings

lecture 10 13

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(1) ENTIRE-NAME

J O H N H S M I T H

FIRST-NAME LAST-NAMEMIDDLE-INITIAL

Page 14: Lecture 101 String Processing Operations on numeric edited fields i.e., view the PIC as a string of characters –Reference modification read/write substrings

lecture 10 14

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

Page 15: Lecture 101 String Processing Operations on numeric edited fields i.e., view the PIC as a string of characters –Reference modification read/write substrings

lecture 10 15

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.

Page 16: Lecture 101 String Processing Operations on numeric edited fields i.e., view the PIC as a string of characters –Reference modification read/write substrings

lecture 10 16

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