Loading

OCCURS Clause (arrays) in Easytrieve

This post is a part of Easytrieve variable declaration. You may consider reading it first.

As in COBOL, OCCURS clause can be used to declare an Array in Easytrieve also.  As in most of the languages, Arrays can be accessed using INDEX or SUBSCRIPS. Using Index is an efficient option. Subscripts are easier to code.
Below is a sample program.

Declare and use an array by Index

when using Index for accessing an array element, you first set the index. In the below example it is set to 15. and in the next step, you read (or write to) that particular indexed element.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE INFILE
 WS-ALPHA W 26 A VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
 WS-ALP-ARR WS-ALPHA 1 A OCCURS 26 INDEX ALPHA-NDX
*
 JOB INPUT NULL
* PRINT 16TH ALPHABET
 ALPHA-NDX = 15
 DISPLAY '16TH ALPHABET IS ' WS-ALP-ARR
 STOP.

Accessing an array by Subscripts 

when using subscript, you can directly read the n-th element of an array – by passing the subscript in brackets.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE INFILE
 WS-DAYS-OF-WEEK W 10 A OCCURS 7
*
 JOB INPUT NULL
 MOVE 'SUNDAY' TO WS-DAYS-OF-WEEK(1)
 MOVE 'MONDAY' TO WS-DAYS-OF-WEEK(2)
 MOVE 'TUESDAY' TO WS-DAYS-OF-WEEK(3)
 MOVE 'WEDNESDAY' TO WS-DAYS-OF-WEEK(4)
 MOVE 'THURSDAY' TO WS-DAYS-OF-WEEK(5)
 MOVE 'FRIDAY' TO WS-DAYS-OF-WEEK(6)
 MOVE 'SATURDAY' TO WS-DAYS-OF-WEEK(7)
 DISPLAY '4 TH DAY IS ' WS-DAYS-OF-WEEK(4)
 STOP.

Want another example?

One of the use of array is to parse a string character by character – Parse a String into words.

Related Posts




blog comments powered by Disqus

There's 26 Comments So Far

Share your thoughts, leave a comment!