Search code examples
loopslogicmainframepl-i

PL/I - How do I read a file in a loop without opening/closing the file multiple times


I have a program that is reading through a file(FILE1). For every record in FILE1 it gets the field 'A' and it searches through FILE2 to find a record with a matching value of field 'B'. When A = B some fields from both files are read out. The program is currently working with code like below. However, the problem is I am opening and closing FILE2 in a loop, multiple times. I've tried this without opening the file inside the loop but if I do that I get repeated records as FILE2 is being read in from where the previous search left off. Is there any way I can point to the beginning of File2 every time I read a new record from FILE1? The code is below:

READ FILE(FILE1) INTO (IN_LAYOUT);
    DO WHILE (MORE_RECS1);
       OPEN FILE(FILE2);
       READ FILE(FILE2) INTO (IN_LAYOUT2);
       MORE_RECS2 = '1'B;
              DO WHILE (MORE_RECS2);
                 IF (A = B) THEN
                  DO;
                     VAL = VAL2;
                     WRITE FILE (OUFILE) FROM (OUT_LAYOUT);
                     S_MORE_RECS2 = '0'B;
                     CLOSE FILE(FILE2);
                  END; /* ENDIF */
                  ELSE READ FILE(FILE2) INTO (IN_LAYOUT2);
              END; /* INNER DOWHILE */
       READ FILE(FILE1) INTO (IN_LAYOUT);
    END; 

Solution

  • This looks like a match-merge. Try first sorting the files by the keys you're matching on.

    At least some mainframe sort utilities have this match-merge functionality built in, Syncsort for example has the JOIN operator. I'm certain DFSORT also has this capability.