Int Function
Int Function
• Usage-Example
– Move FUNCTION CURRENT-DATE to CURR-DT
– If FUNCTION DATE-OF-INTEGER(base-date)
– When FUNCTIO DAY-OF-INTEGER(base-date)
• Examples
– Current-date, Length, Lower-case, Date-of-integer
Intrinsic Functions
• DATE
– 77 X1 PIC 9(6) VALUE 0.
– ACCEPT X1 FROM DATE
– DISPLAY X1
– DATE will be YYMMDD ie PIC 9(6) format
• TIME
– ACCEPT X1 FROM TIME
– TIME will be HHMMSSCC ie PIC 9(8) format
• DAY
– ACCEPT X1 FROM DAY
– X1 PIC 9(3) ie 1-365 format
• DAY-OF-WEEK
– ACCEPT X1 FROM DAY-OF-WEEK
– Value ranges from 1 to 7
Intrinsic Functions
• Date Format : Range: January 1, 1601 to December 31, 9999
• Notes:
– Before we examine date type intrinsic functions, we need to define three basic
date formats that COBOL can work with
• INTEGER-OF-DATE
– COMPUTE integer-date = FUNCTION INTEGER-OF-DATE (20210218)
– Gregorian-Date must be in the form of YYYYMMDD.
– The function result is a 7 digit integer
– Year must be 1600 < YYYY < 9999 & 0 < MM < 13 & 0 < DD < 32
• DATE-OF-INTEGER
– COMPUTE Gregorian-Date = FUNCTION DATE-OF-INTEGER (Integer-Value)
– The function results Gregorian-date of 8 digit integer in the form of
YYYYMMDD
Intrinsic Functions
• INTEGER-OF-DAY
– COMPUTE Integer-Date = FUNCTION INTEGER-OF-DAY
(2021049)
– Julian Date must be in form of YYYYDDD
– The function result 7 digit Integer
– Year must be 1600 < YYYY < 9999 and 0 < DDD < 367
• DAY-OF-INTEGER
– COMPUTE Julian-Date = FUNCTION DAY-OF-
INTEGER(Integer-Date)
– Julian Date is in form of YYYYDDD and 7 digit Integer
– Integer-Date represents a number of days after December 31,
1600 in Gregorian calendar.
Intrinsic Functions
• LENGTH
– MOVE IN-EMPREC (1: FUNCTION LENGTH (ABC)) TO PQR.
– COMPUTE PQR = FUNCTION LENGTH(ABC)
• Returns the length of 77 ABC PIC X(10) , the declaration size.
• MOVE FUNCTION LENGTH(ABC) not allowed.
• LOWER-CASE
– MOVE FUNCTION LOWER-CASE (ANSWER) TO Y.
• UPPER-CASE
– MOVE FUNCTION UPPER-CASE (ANSWER) TO Y.
• NESTING FUNCTION
– COMPUTE NEW-DUE-DATE = FUNCTION DATE-OF-
INTEGER(FUNCTION INTEGER-OF-DATE(DATE-OF-ORDER
+ 30))
Intrinsic Functions
• REM – Returns remainder for the input non-integer value.
– COMPUTE X = FUNCTION REM (D, E)
• MOD
– Returns the remainder for the input integer value
• COMPUTE X = FUNCTION MOD (D, E)
• MIN
– Lowest value in the list of values
– COMPUTE X = FUNCTION MIN (D, E, F)
• FACTORIAL
– COMPUTE X = FUNCTION FACTORIAL (D)
• REVERSE
– MOVE FUNCTION REVERSE(Y) TO Q
STRING HANDLING
Introduction to String handling
• Programs often require that data be moved from one area to
another and / or that operations be performed on the individual
characters of a data set.
• 77 ABC PIC X(20) VALUE ‘COFORGE’.
• 77 PQR PIC X(10) VALUE ‘TECHNOLOGIES’.
ALL identifier-3
FOR LEADING literal-1
CHARACTERS
BEFORE identifier-4
INITIAL
AFTER literal-2
• LEADING
– COUNTS/REPLACE all compare $1 character from the first
valid one encountered to the first invalid one.
• FIRST
– REPLACES only the first valid characters.
• BEFORE
– Designates characters to the left of delimiter as valid
• AFTER
– Designates character to the right of delimiters as valid
Example
• INSPECT ID-1 TALLYING ID-4 FOR ALL “F” BEFORE INITIAL “G’.
– ID-4 value is 3
Example contd
CHARACTERS BY {id-2/lit-1}
ALL
LEADING {id-4/lit-3} BY {id-5/lit-4}
FIRST
BEFORE INITIAL {id-6/lit-5}
AFTER
Example
• Syntax
INSPECT identifier-1 TALLYING
<tallying part as in format 1>
REPLACING
<replacing part as in format 2>.
• Example
– 77 ID-1 PIC X(15) VALUE 'FFFBCGHIFFFJKL'.
– 77 ID-2 PIC 99 VALUE 0.
– INSPECT ID-1
TALLYING ID-2 FOR CHARACTERS BEFORE INITIAL “I”
REPLACING CHARACTERS BY “*” AFTER INITIAL “I”.
• ID-1 value is FFFBCGHI*******
• ID-2 value is 07
Format 4 – Syntax & Example
• INSPECT <identifier1> CONVERTING <identifier2/literal1> TO
<identifier3/literal2>
BEFORE/AFTER INTIAL < identifier4/literal-3>
Example
[ ON OVERFLOW imperative-statement ]
STRING
Ident1 DELIMITED BY SIZE
Ident2 DELIMITED BY SPACES
Ident3 DELIMITED BY "Frogs"
INTO Ident4
WITH POINTER StrPtr
END-STRING.
STRING verb
• DELIMITED BY SIZE
The DELIMITED BY SIZE clause means that the whole of the
sending field will be added to the destination string.
• ON OVERFLOW
If the ON OVERFLOW clause is used then the statement
following it will be executed if there are still characters left to
pass across in the source field(s) but the destination field has
been filled.
• WITH POINTER
The WITH POINTER phrase allows an identifier/dataname to be
kept which holds the position in the Destination String where the
next character will go.
• When the WITH POINTER phrase is used, the program must set
the pointer to an initial value greater than 0 and less than the
length of the destination string before the STRING statement
executes.
• If the WITH POINTER phrase is not used, operation on the
destination field starts from the leftmost position.
Example 1
01 name-in.
05 first-name pic x(10) value 'Mahender '
05 Last name pic x(10) value 'Reddy '
05 initial pic x(2) value 'G ‘
01 NAME-OUT PIC X(25) VALUES SPACES.
STRING
FIRST-NAME DELIMITED BY SPACE
LAST-NAME DELIMITED BY SPACE
INITIAL INTO NAME-OUT.
MOVE 6 TO StrPtr.
01 StringFields.
02 Field1 PIC X(18) VALUE "Where does this go".
02 Field2 PIC X(30)
VALUE "This is the destination string".
02 Field3 PIC X(15) VALUE "Here is another".
02 Field4 PIC X(20) VALUE SPACES.
DISPLAY Field4
Displays WHERETHISHERE
Example 6
UNSTRING id-1
PROCEDURE DIVISION.
MAIN-STREET.
MOVE 'ROGERS WILLIAM THOMAS' TO WHOLE-NAME.
UNSTRING WHOLE-NAME
DELIMITED BY SPACE
INTO LAST-NAME FIRST-NAME MIDDLE-NAME.
DISPLAY 'FIRST NAME: ' FIRST-NAME.
DISPLAY 'MIDDLE NAME: ' MIDDLE-NAME.
DISPLAY 'LAST NAME: ' LAST-NAME.
Displays FIRST NAME: WILLIAM
MIDDLE NAME: THOMAS
LAST NAME: ROGERS
Example 1 A
• Multiple Delimiters
UNSTRING INREC
DELIMITED BY ',' OR SPACE
INTO A1, B1, C1.
Result:
WW-U1 --
WW-U2 --BBB
WW-U3 -- CC
WW-CH-1--02
WW-CH-2--03
WW-CH-3--03
Note – Count takes Spaces also into consideration.
Example 6
1 9 -0 5 - 8 0
ACCEPT DateStr.
UNSTRING DateStr
INTO DayStr, MonthStr, YearStr
ON OVERFLOW DISPLAY “OVERFLOWING“.
Displays OVERFLOWING
Example 7
• Multiple delimiters by Using OR in UNSTRING
Result:
WW-U1 --AA
WW-U2 --BBB
WW-U3 --CC
WW-CH-1--02
WW-CH-2--03
WW-CH-3--02
Example 8
01 DayStr PIC XX.
01 MonthStr PIC XX.
01 YearStr PIC XX.
01 DateStr PIC X(8). 1 9 - 0 5 / 8 0
01 HOLD1 PIC XX.
01 HOLD2 PIC XX.
ACCEPT DateStr.
UNSTRING DateStr
DELIMITED BY "/" OR "-"
INTO DayStr DELIMITER IN Hold1
MonthStr DELIMITER IN Hold2
YearStr.
DISPLAY DayStr SPACE MonthStr SPACE YearStr.
DISPLAY Hold1 SPACE Hold2
Displays 19 05 80 and HOLD1 is - , HOLD2 is /
Example 9
• UNSTRING with Tallying Option
– It gives the count of number of receiving fields. If there are 4 fields in INTO
clause then the count would be 4.
RESULT
• In this case WW-TL-1 contains a value of 4.