You are on page 1of 15

IBM Mainframes

COBOL Training Class-11

Understand the Problem

01 TEMP-REC.

05 ONE-AM PIC S9(3). 05 TWO-AM PIC S9(3). 05 THREE-AM PIC S9(3). 05 FOUR-AM PIC S9(3). 05 MIDNIGHT PIC S9(3). CALCULATION: COMPUTE AVG-TEMP ( ONE-AM ! TWO-AM ! THREEAM ! . ! MIDNIGHT) " #$ .

Introduction to OCCURS Clause

We use an OCCURS Clause in COBOL to indicate the repeated occurrence of fields with same format (Type & Size). Some of the ses are!

"efinin# a series of $%O fields of same format "efinin# a series of totals in wor&in#'stora#e section "efinin# a ta(le to accessed (y each record.
)* T+,-'.+C. )/ WS'T+,- OCC .S 01 T$,+S -$C S2(3). )* 4..45'* 64L + 7+.O. )/ TOT4LS OCC .S /) T$,+S -$C 2(/).

Rules to use

4n occurs clause may (e used on le8els )0 9 12


only i.e.: occurs is not 8alid for )*%;;%<<%== le8el

)* $>'.+C

OCC .S */ T$,+S.

)* $>'.+CO.".
)/ $>'.+C OCC .S */ T$,+S.

Subscri t

A SUBSCRIPT is used in the procedure division to indicate which specified item within the array we wish to access. We use subscript a on! with the identifier that is defined with in occurs " to refer to an item with in the array #$amp e% !CC"PT #S-T"MP $1% & !CC"PT #S-T"MP $'(% In between & and '( on y but not ) or '*. Rules % one space after the variab e and eft parenthesis and no space after or before the parenthesis.

#e can resol)e the other

roblem

+,-# & T, SUB. +,-# .#R,S T, T,TA/0T#+P. P#R1,R+ U2TI/ SUB 3 '( A44 WS0T#+P 5 SUB6 T, T,TA/0T#+P A44 & T, SUB #240P#R1,R+.

P#R1,R+ CA/0PARA -AR7I28 SUB 1R,+ & B7 & U2TI/ SUB 3'(. CA/0PARA C,+PUT# T,TA/0T#+P 9 T,TA/0T#+P : WS0T#+P 5SUB6 .

COBOL OCCURS***+"P",+I,- O,
This is similar to the COBOL OCC .S clause: e?cept the num(er of times it occurs 8aries from record to record. )/ L$>+'$T+,'CO >T -$C 22. )/ L$>+'$T+,S OCC .S ) TO 0/ T$,+S "+-+>"$>@ O> L$>+'$T+,'CO >T. *) A 4>T$T5 -$C 2222. *) "+SC.$-T$O> -$C B(3)). *) >$T'-.$C+ -$C S2(/)622. The ta(le L$>+'$T+,S contains three fields A 4>T$T5: "+SC.$-T$O>: >$T'-.$C+) which ma&e up one line on an in8oice. +ach in8oice can ha8e from ) to 0/ line items.

S"!RC. T"C.,I/U"S

There are 0types of search techniCues. S+4.CD & S+4.CD 4LL

Search is used to find on element from the ta(le.

SEARCH: $n Search the records will (e chec&ed in a linear or seCuential fashion until a match is found or until end of the ta(le (+OT). SEARCH ALL: $n Search all it di8ides the ta(le into 0 eCuals half and it chec&s for the element either in first half or second half (ased on element. $t &eeps on di8idin# the ta(le into 0 eCual half until a match is found or until ta(le loo&up is completed.

The I,+"0"+ B1 Clause

When usin# a S+4.CD statement: ta(le entries must (e specified with an inde? rather than a su(script. 4n inde? is similar to a su(script: (ut it is defined alon# with the ta(le entries as part of the OCC .S description!

)* S4L+S'T4B'T4BL+. )/ T4BL+'+>T.$+S OCC .S *))) T$,+S $>"+B+" B5 B*. *) WS'7$-CO"+ *) WS'T4B'.4T+ -$C 2(/). -$C 6222.

S"!RC.
DATA DIVISION. 01 ACCT-DETAILS. 05 ACCT-DETAILS OCCURS 100 TIMES INDE%ED &' A1. 10 ACCT-NO PIC 9(10). 10 CUST-NAME PIC %(#0). (( WS-NAME PIC %(#0) VALUE SPACE. PROCEDURE DIVISION. SEARCH-PARA. SET A1 TO 1. SEARCH ACCT-DETAILS AT END DISPLA' )RECORD NOT FOUND* WHEN WS-NAME CUST-NAME(A1) DISPLA' ACCT-DETAILS(A1) END-SEARCH.

S"!RC. !LL
DATA DIVISION. 01 ACCT-DETAILS. 05 ACCT-DETAILS OCCURS 100 TIMES ASCENDING +E' IS ACCT-NO INDE%ED &' A1. 10 ACCT-NO PIC 9(10). 10 CUST-NAME PIC %(#0). (( WS-NAME PIC %(#0) VALUE SPACE. PROCEDURE DIVISION. SEARCH-PARA. ACCEPT WS-NAME. SEARCH ALL ACCT-DETAILS AT END DISPLA' )RECORD NOT FOUND* WHEN WS-NAME CUST-NAME(A1) DISPLA' ACCT-DETAILS(A1) END-SEARCH.

SEARCH LINEAR " SE,UENTIAL SEARCH.

SEARCH ALL &INAR' SEARCH.

I- ./0123 1/2415. -//5 -46 7/ 8- .416/5 415/1.

R/2415. .349:5 7/ 8- .416/5 415/1.

W/ 30;/ 64 ./6 8-5/< ;01807:/ 64 1 7/=41/ N4 -//5 64 ./6 8-5/< ;01807:/ 64 1A 0. 63/ >/1=41?8-@ ./0123. 1/2415. 01/ 8- .416/5 415/1. A-B :4@820: 4>/10684- 20- 7/ >/1=41?/5. ( CA DA C A D A N46 ) M9:68>:/ )E3/-* 24-58684-. 20- 7/ 245/5. S/0123 8. >1/=/107:/ E3/- 607:/ 8. .?0::. W/ 54-*6 245/ ASC-+E' OR DSC-+E'. O-:B 4>/10641 8. >4..87:/.

O-:B .8-@:/ )E3/-* 24-58684- 20- 7/ 245/5. P1/=/107:/ E3/- 607:/ 8. :01@/. S349:5 245/ ASC-+E' OR DSC-+E'.

+ifference Bet2een
SU&SCRIPT O22911/-2/: 86 8-6/1-0::B 24-;/16. 64 58.>:02/?/-6 0-5 =/623/. 63/ ;0:9/. S:4E/1. S97.218>6 8. E41F8-@-.6410@/ ;01807:/. I6 .349:5 7/ 5/2:01/5 8- E41F8-@-.6410@/ ./2684-. I6 20- 7/ I-21/?/-6/5"D/21/?/-6/5 E863 24?>96/ 41 >/1=41?. COMPUTE + +!1 (OR) COMPUTE + +-1 (OR) PERFORM VAR'ING. INDE% D8.>:02/?/-6: 86 34:5. 63/ 0551/.. 4= 63/ ;0:9/ 0-5 581/26:B =/623/. 63/ ;0:9/. F0.6/1. I-5/< 8. 0- 8-6/1-0: 86/?. N4 -//5 64 5/2:01/ 8-5/< ;01807:/. T4 8-21/?/-6 8-5/< E/ 30;/ 64 9./ SET ;/17. SET INDE%-VAR TO 1 OR SET INDE%-VAR UP &' 1"(+ +!1) OR SET INDE%-VAR DOWN &' 1"(+ +1)

Limitations of the S"!RC. !LL Statement


*. The condition followin# the word WD+> can test only for eCuality!
V0:85: WHEN T-CUSTOMER-NO (%1) CUST-NO-IN I-;0:85: WHEN T-WEIGHT-MA% (%1) D WEIGHT-MAILED

0. . Only one WD+> clause can (e used with a S+4.CD 4LL. 3. The 64.5$>@ option may not (e used with the S+4.CD 4LL. 1. The OCC .S item and its inde?: which define the ta(le ar#ument: must appear to the left of the eCual si#n.
V0:85: WHEN S-AMT (%1) AMT1... I-;0:85: WHEN AMT1 S-AMT (%1)...

Than3 1ou

You might also like