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.

Advertisement

28 thoughts on “OCCURS Clause (arrays) in Easytrieve”

  1. is there any way to close a file ?
    (i need to print the no of records count of input file as the output file’s first record and then proceed by printing all the records from input file to the output file)
    for this i need to open the file and read it fully to find the record count and then close it. now i need to open it again so that i can print all its records to output file.
    occurs can be used. but i need to know if there is a way to close a file???? (in easytrieve)

  2. File1:

    A111
    B222
    C333
    D444

    File2:
    A111CC
    A111DD
    A111EE
    B222AA
    B222FF
    C333GG
    C333II
    C333XX
    C333YY

    OUTPUT:

    A111 CCDD
    B222 AAFF
    C333 GGIIXXYY

    Can you please let me know how we can write the above output by using easytrieve?

  3. LIST ON
    FILE FILE1
    KEY1 1 4 A
    FILE FILE2
    KEY2 1 4 A
    VAL2 5 2 A
    FILE OUTPUT
    OUTKEY 1 4 A
    OUTVAL 5 20 A
    *
    WS-DATA W 20 A
    WS-OUT WS-DATA 2 A OCCURS 10
    WS-INDX W 2 N
    JOB INPUT NULL
    GET FILE1
    GET FILE2
    WS-INDX = 1
    DO WHILE NOT EOF FILE1
    DO WHILE KEY2 LE KEY1 AND NOT EOF FILE2
    IF KEY1 EQ KEY2
    MOVE VAL2 TO WS-OUT(WS-INDX)
    WS-INDX = WS-INDX + 1
    END-IF
    GET FILE2
    END-DO
    IF WS-INDX GT 1
    MOVE KEY1 TO OUTKEY
    MOVE WS-DATA TO OUTVAL
    PUT OUTPUT
    END-IF
    GET FILE1
    MOVE SPACES TO WS-DATA
    WS-INDX = 1
    END-DO
    STOP
    *

    Output:

    A111CCDDEE
    B222AAFF
    C333GGIIXXYY

  4. Thank you. Its a awesome blog. I appreciate you for putting efforts on this blog. My suggestion is if you put more example programs in this site that could be great.

  5. Hi Mr Karthik,

    Your blog helps me out while coding in easytrieve.
    I am a fresher working in the same and would like to know more on the usage of arrays.

    Could you help me out.

    Bhavana.K

  6. Hi Bhavana,

    Thanks for your comment. Arrays can be used for multiple purposes.
    – to work with a set of values of the same data type
    – to store some lookup/reference data
    – when you want to have a pointer(index) and process a string character by character

  7. hey thank you..

    I had to write a code to split the given phrase when a space occurs in it.

    Input could be
    Thank You for the comment.

    While my output should be
    Thank
    You
    for
    the
    comment.

    i had tried using the arrays concept with a row and col index.but dint get it right. could you help me out with this. i just need to know the logic of arrays usage in this and try on coding it.

  8. Hi Mr.Karthik.

    Thank you, that code was really did help me out.
    your blog show out the experience you have in this field.

    Thank you once again.

    Regards,
    Bhavana.K

  9. Hi, guys!

    Anybody knows how could I declare an array using depending on, in Easytrieve?

  10. I don’t think we can declare such arrays in Easytrieve.

    However, we can have a variable that can have a dynamic length using VARYING clause.
    and we need to use:DATA, :LENGTH to change the data / length of the variable.

    DEFINE WS-VAR W 20 A VARYING
    JOB INPUT NULL
    WS-VAR:LENGTH = 5
    MOVE ‘TEXT1’ TO WS-VAR:DATA
    DISPLAY WS-VAR
    STOP

  11. hey..I need help on this urgently.
    I want to copy entire idms table in a ps file.
    need to do this for many tables & store each one in different ps file.

    is there any way I can do this in loop?
    I am not able to do this since “obtain” record needs record name and area explicitly written..
    I tried using occurs clause, but no use 😦

  12. Hi,

    How are you reading the idms table from Easytrieve? Can you please share your sample program?

  13. hi,
    thanx for the quick response ..although I won’t b able to share the code 😦 I can explain my 2 doubts ..

    1.
    I am reading the tables using OBTAIN command
    but it requires table name n area name to b written in quotes..where I actually want to use a variable..so that I can loop the process for multiple tables

    or is there any other method to read where this might b poss?

    2.
    since m writing the idms records from different tables into different PS files, how wil I refer to them in the loop

    I thought OCCURS was the solution to all, but its nt as easy as I expected..
    for now I have just copied the code manyyyy times with different table name, table area & different PS file name. but I would still like to automate the process if it’s possible..

    thanx a lot in advance for the help πŸ™‚
    it’s really difficult to find help on mainframe.
    this is the only forum I found active.

  14. Hi Pallavi,

    To automate such process, I would suggest you to have two easytrieve steps in a job.

    1. First step, reads two files, one file containing the list of IDMS table names, second file containing DD names of the PS files.
    This easytrieve step will create a Easytrieve program in the output file, having the repeated code for each table.

    2. Second step executes the Easytrieve program created in First step, and should contain all the DD statements with output file names.

    This just reduces the manual effort to repeat the code, and avoids any human error while doing so.

    I did not ask you to share the full code. Just a sample code with the snippet explaining how the table is read.

  15. hmm.. yes I got that. but as am accessing the internet on phone, I don’t hav the snippet to share..n typing so much wud take time ..

    so this is the max v can do to automate. ohk den πŸ™‚ I thought there might b a bttr way n since m New to thus, I don’t know..

    thanx for da help though πŸ™‚

  16. I cannot find any better way than that.
    1. Since you say you are giving the table name in quotes for OBTAIN statement, you can use a variable and populate the table name there. (I have not used OBTAIN statement)

    2. But I don’t think we can give a DD name through a variable and write data to it. Thats why recommended you to dynamically create the Easytrieve program and execute it in the next step.

  17. Hi Karthi,

    using easytrieve
    I need to fetch a record from one file one by one and need check whether it is present in other file.

    it has to read the entire second file. and again have to fetch the second record from first and start reading the 2 nd file from first.

    Thank you!

  18. Hi,

    Is it possible to initialize an array (entirely – at the point of declaration) in easytrieve? If yes Can you please let me know how? I tried using Value keyword in the array declaration statement. But got the following error:

    B016 INVALID OR CONFLICTING KEYWORD – VALUE

  19. Hi,
    How to intialize an array after writing data to it. The problem is i can declare array with only 32000 size, so 32001 th record can’t be written into array. The req is in input file i will have records with invoive no. now load all data into array for 1st invoive no and then for next invoive no i hv to reset the array and load once again data corresponding to invoice no. Do you have any idea how to reset the array?

  20. Hi Karthik,

    using easytrieve
    I need to fetch a record from one file one by one and need check whether it is present in other file.

    it has to read the entire second file. and again have to fetch the second record from first file and start reading the 2 nd file from first.

  21. Hi,

    I have input file where a number starts from 8th position. I just want the next nine numbers from input file(i.e. 8th,9th,10th..) . I also need to swap the number to my output file. usually the 9 numbers are in 5-2-2 format(56958-25-36). i need that nine no be arrange in 2-2-5 format(25-36-56958). Please help me out……….

  22. Hi.
    Can someone please help me how to print like this

    Input 004 002 000 000 000
    Output 004 002

  23. Hi Karthik,

    I am new to easytrieve. How occurs with index works. I am working on easytrieve to cobol conversion project.

    I have following code in easytrieve.
    MIS-FED W 4 P 0 INDEX(MIS-I3) MASK (C ‘$,$$$,$$$’) OCCURS 12

    In activity section the code is as follows.
    MIS-I3 = ( MM-CAN – 1 ) * 4 where MM-CAN is month.
    MIS-FED = MIS-FED + FED-TAX

    Please help me to understand the above code.
    Thank you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.