COBOL .

COBOL In Line Perform

Working-Storage

000340 01  CAT-TYPE    PIC X(15) VALUE 'CALICO'.
000360 01  CAT-COUNT   PIC 99 VALUE 15.  

InLine Perform Example Number 1

004310
004320     PERFORM UNTIL CAT-TYPE(CAT-COUNT:1) NOT = SPACE
004325         OR CAT-COUNT < 1
004330         SUBTRACT 1 FROM CAT-COUNT
004340     END-PERFORM.
004440
This finds the length of CAT-TYPE.
Note: COBOL will ordinarily test first (check if CAT-TYPE(CAT-COUNT:1) NOT = SPACE, and then perform the subtract. If you want it to do things in the reverse order use WITH TEST LAST.

InLine Perform Example Number 2 - Using TEST LAST

004310
004320     PERFORM UNTIL CAT-TYPE(CAT-COUNT:1) NOT = SPACE
004323         OR CAT-COUNT < 1
004325         WITH TEST LAST
004330         SUBTRACT 1 FROM CAT-COUNT
004340     END-PERFORM.
004440
However, doing things in this order would not test byte 15 of CAT-TYPE.
Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate.
Comment by Fumigator on 2013-06-10 Rate this Comment

Just a nit-pick here-- You should always test the iterator of a loop first before anything else in a PERFORM condition, to avoid an array overflow. For example:

PERFORM UNTIL CAT-COUNT < 1
OR CAT-TYPE(CAT-COUNT:1) NOT = SPACE
..SUBTRACT 1 FROM CAT-COUNT
END-PERFORM

Since the PERFORM loop bails out as soon as a condition is met, then the condition "CAT-TYPE(0:1) NOT = SPACE" will never be tested, and you'll avoid an index out of range error.

Example number 1 will throw an error if CAT-TYPE is all spaces.

While I'm at it, I may as well mention that it's a good idea to use the "VARYING var FROM int BY int" syntax when dealing with an iterator-- just makes sense.

PERFORM VARYING CAT-COUNT FROM 15 BY -1
UNTIL CAT-COUNT < 1
OR CAT-TYPE(CAT-COUNT:1) NOT = SPACE
END-PERFORM

Comment by TheMadProfessor on 2013-05-22 Rate this Comment

Can't do anything about your incipient baldness problem - looking for the solution to that one myself. The problem with the code I see is that you have two IFs contained within the PERFORM, but only one END-IF terminator. Since you cannot have an overt terminator (period) within the body of the inline PERFORM, all potentially multi-statement clauses (READs with AT END, COMPUTEs with ON OVERFLOW, EVALUATEs, IFs, etc.) must have its corresponding terminator so the compiler can determine to which clause each statement belongs, even if the clause consists of just a single statement. There may be some other problem due to various compiler options or other code surrounding the below snippet, but I suspect adding a second END-IF will eliminate the error.

Comment by archive on 2007-09-28 Rate this Comment


I was hoping you could help me resolve this error before i go completely
bald :(


PERFORM UNTIL WS-TRANSACTION-KEY = HIGH-VALUES
AND WS-MASTER-KEY = HIGH-VALUES
IF WS-MASTER-KEY < WS-TRANSACTION-KEY
PERFORM 300-WRITE-OUTPUT
MOVE ZEROS TO WS-NUM-TRANSACTIONS
PERFORM 100-READ-MASTER
ELSE IF WS-MASTER-KEY = WS-TRANSACTION-KEY
ADD 1 TO WS-NUM-TRANSACTIONS
PERFORM 200-READ-TRANSACTION
ELSE
PERFORM 200-READ-TRANSACTION
END-IF
END-PERFORM

/***************************************************************************/

this is the error i'm getting.....
IF WS-MASTER-KEY < WS-TRANSACTION-KEY
............^
%COBOL-E-CNDNOGOOD, Conditional statement detected - Imperative statement
requir
ed
at line number 92 in file DKB200:[ANGEREVM]MASTER_UPDATE.COB;48
%COBOL-E-ENDDIAGS, DKB200:[ANGEREVM]MASTER_UPDATE.COB;48 completed with 1
diagno
stic

Comment by rahul on 2013-05-22 Rate this Comment

inline perform is really a very stong method. it is same as the do while loop in c.

 
Sign in to comment on COBOL In Line Perform.