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

  •   Manigandan Selvaraj
    November 30th, 2010 at 4:39 am

    Simply Spoon- feeding….. Simply SUPERB…

  •   Karthik
    January 13th, 2011 at 4:08 pm

    Thanks for the comment, boss!

  •   ARSHAD
    December 29th, 2011 at 7:18 am

    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)

  •   Bhargav
    April 26th, 2012 at 7:04 pm

    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?

  •   Karthik
    April 27th, 2012 at 5:30 am

    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

  •   Bhargav
    April 27th, 2012 at 5:57 pm

    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.

  •   Bhavana K
    May 19th, 2012 at 5:39 pm

    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

  •   Karthik
    May 20th, 2012 at 5:47 am

    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

  •   Bhavana K
    May 20th, 2012 at 10:40 am

    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.

  •   Karthik
    May 20th, 2012 at 10:57 am

    Hi Bhavana,

    I wrote one program to split a variable that has Full name (First, Middle, Last names) in a string, that need to be split into three variables. I have just posted it in http://code.xmlgadgets.com/2012/05/20/easytrieve-program-to-parse-a-string/

    Can you please have a look? I think it should help you.

  •   Bhavana
    May 21st, 2012 at 5:17 am

    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

  •   Karthik
    May 21st, 2012 at 5:37 am

    Thanks Ms.Bhavana !

  •   Marcus Rioli
    June 20th, 2012 at 8:02 pm

    Hi, guys!

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

  •   Karthik
    June 21st, 2012 at 9:24 am

    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

  •   pallavi
    October 5th, 2012 at 9:42 am

    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 :(

  •   Karthik
    October 6th, 2012 at 11:55 am

    Hi,

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

  •   pallavi
    October 7th, 2012 at 1:18 pm

    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.

  •   Karthik
    October 8th, 2012 at 5:07 am

    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.

  •   pallavi
    October 8th, 2012 at 6:08 am

    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 :)

  •   Karthik
    October 8th, 2012 at 6:31 am

    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.

  •   Mani
    December 19th, 2012 at 6:37 pm

    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!

  •   Bhu
    September 24th, 2013 at 9:32 am

    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

  •   Karthik
    September 25th, 2013 at 9:41 am

    You have to redefine the array into another variable that you can initialise with a VALUE clause

  •   karthik
    October 16th, 2013 at 1:02 pm

    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?

  •   Sujoy
    November 10th, 2013 at 8:09 pm

    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.

  •   Sathish
    November 19th, 2013 at 2:48 pm

    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……….

Share your thoughts, leave a comment!