Menu

Sorting and merging of FILES

Anonymous
2016-10-03
2020-08-17
  • Anonymous

    Anonymous - 2016-10-03

    I am new to COBOL. Can anyone tell me the program to compare two files (File 1 and File 2) and write matching records to a file (Outfile 1) and unmatched to another File (Outfile 2) in mainframe.

     
    • cdg

      cdg - 2020-08-10

      See attached.

       
  • Bill Woodger

    Bill Woodger - 2016-10-03

    You could search-engine for "cobol two file match". Bear in mind, it is the internet, so there will be a fair amount of cr... variance in quality out there. If you find something you feel comfortable with, post the link or the code here, and we can comment :-)

     
  • Gary Van Winkle

    Gary Van Winkle - 2020-08-09

    I'm trying to do a file SORT with INPUT PROCEDURE and OUTPUT PROCEDURE. In order to expedite testing I'm not using any files, except the Sort Work File itself. I'm trying to populate a Sort Record from an array of records, RELEASE each one in turn. When the SORT is complete, I want to display each sorted record to simplify checking the results. I'm having trpuble in connecting all the dots required for the sort process. Here's my code. See the comments, please.

    DATA DIVISION.
    
      FILE SECTION.  
    SD  SORT-FILE.     
    01  SORT-RECORD. 
        03  SR-SEASON      PIC 9.
        03  SR-ID          PIC 999.
        03  DATA           PIC X(34).
    
    PROCEDURE DIVISION.
    
    SET ENVIRONMENT "TEMP" TO "C:\TEMP-FOLDER". *> My C:\TEMP-FOLDER EXISTS.
    
    SORT  SORT-FILE  ON  ASCENDING KEY  SR-SEASON
                         ASCENDING KEY  SR-ID
                INPUT PROCEDURE  0100-INPUT   THRU  0199-EXIT
               OUTPUT PROCEDURE  0200-OUTPUT  THRU  0299-EXIT.
    
    0100-INPUT.
    
    *> PERFORM A LOOP TO POPULATE SORT-RECORD, 
    *>   THEN RELEASE IT TO THE SORT. 
    
          RELEASE  SORT-RECORD *> IGET:
    *> "error: permanent file error (status = 30) for file SORT-FILE
    *> ('SORT-FILE') on RELEASE.
    
        END-PERFORM.
    0199-EXIT.
      EXIT.
    
    0200-OUTPUT.
      MOVE  1  TO  OUT-SUB.
    
      RETURN  SORT-FILE  INTO  WS-COPY-TABLE (OUT-SUB)
        AT END  GO TO  0300-DISPLAY
      END-RETURN.
    
      ADD  1  TO  OUT-SUB.
    0299-EXIT.
      EXIT.
    

    Prog Guide says I need to SET ENVIRONMENT for a temporary folder. I tried to use TMPDIR, then TMP, then TEMP with the same results, but I prefer to use my own folder. So, what works?

     

    Last edit: Simon Sobisch 2020-08-11
    • cdg

      cdg - 2020-08-10

      You've left out important parts of the program, so it's difficult to diagnose your problem. The error status 30 is probably complaining about the sort file name, which should have been defined something like:

      INPUT-OUTPUT SECTION.
      FILE-CONTROL.
          SELECT SORT-FILE   ASSIGN TO "SRTWORK".
      

      with something like the following in your "jcl":

       set  srtwork=d:\temp\srtwork
      

      If I recall, if the file name is invalid, or the file exists, but is incompatible with the access mode, you will get error status 30.

      I also note that you have "at end go to 0300-display" following your return statement. If 0300-display is outside the scope of the perform (0200-output thru 0299-exit) your program will not work properly. NEVER use go to to leave the range of a perform loop.

       

      Last edit: Simon Sobisch 2020-08-11
      • Anonymous

        Anonymous - 2020-08-11

        NEVER use go to to leave the range of a perform loop

        That was one my my first lessons. Screws up all sorts of things.

         
  • Anonymous

    Anonymous - 2020-08-10

    What I'm trying to resolve is the assignment of my choice of file name, i.e. "SORT-FILE", to a folder on the C-drive. I don't know whether I'm allowed to use my own file name or if I must use some reserved word file name (undocumented). I understand INPUT-OUTPUT SECTION & FILE-CONTROL precede the SELECT statement. Also, I've used ASSIGN TO EXTERNAL DISK with a full drive\folder\filename.extension successfully in the past(without the need for "jcl"); but this is the first time I've tried to use SORT file.
    Now that we're past that, I take it that setting a TEMP variable in the Evironment is a way to communicate to the SORT where to create the work file (which I'm trying to specify). If that's true, then I don't uinderstand why "jcl" should be involved. Please illuminate.

     
    • cdg

      cdg - 2020-08-10

      You've still left out the information we need to answer your question.
      The "Select" statement specifies the (path and) name of the file, or an environmental variable that can be set to that name, or a data item that can be set to that name. What do you have in your select statement?
      "SORT-FILE" is the name that you reference in the COBOL program for OPEN, READ, and CLOSE statements, NOT the name of the physical file.
      If you code the select statement EXACTLY as I've shown above, and include the "Set" statement EXACTLY as shown above in the bat/cmd file that runs the program, your program should work (assuming it is otherwise sound). Alternately you can change "SRTWORK" to whatever you choose in both the program and bat/cmd file, and it should still work.
      You can also omit the set statement, and it should create a sort work file by that name (i.e. SRTWORK) in the default path.
      Setting an environmental variable is "jcl".

       
  • Vincent (Bryan) Coen

    In most of my sort processes within a Cobol program I use my defaults as set up in .bashrc

    This shows :
    export TMPDIR=~/tmp

    there is also a export TMP=/tmp

    In th sort file settings it is :

    *>
         select   SortFile assign Sort1tmp.
    *>
    
    *>
     sd  SortFile.
     01  filler.
         03  SdSortKey         pic x(104). 
    

    *>

    In PD for this file I have :

    *>  Report Phase
    *>
         sort     SortFile
                  ascending key SdSortKey
                  using  Supplemental-Part1-Out
                  giving Supplemental-Part2-In.
    

    Where both in and out files are defined. There are many other sorts but most of these are table sorts as a JIC as they should be already but . . .

    .Now just after program start I have :

     accept   Temp-PathName from Environment "TMPDIR".
     if       Temp-PathName = spaces
              accept Temp-PathName from Environment "TMP"
              if  Temp-PathName = spaces
                  accept Temp-PathName from Environment "TEMP"
                  if  Temp-PathName = spaces
                      move "/tmp" to Temp-PathName.
     if       Temp-PathName (1:1) = "/"   *> Its Linux/Unix
              move "/" to OS-Delimiter.
     if       Temp-PathName (1:1) = "\"   *> Its Windoz *> "
              inspect Temp-PathName replacing all "/" by "\"   *> in case of /tmp "
              move "\" to OS-Delimiter.  *> "
     string   Temp-PathName delimited by space
               OS-Delimiter delimited by size
                "Part1.tmp" delimited by size   into Supp-File-1.
     string   Temp-PathName delimited by space
               OS-Delimiter delimited by size
                "Part2.tmp" delimited by size   into Supp-File-2.
     string   Temp-PathName delimited by space
               OS-Delimiter delimited by size
                 "Sort1tmp" delimited by size   into Sort1tmp.
    

    Note that the source (input file) for the sort is not that large 5 Mb and the in and out files are similar. There is no working files present after the program finishes.

    There is no JCL for micros based computers under Windows or Linux that help control usage of file processing well not unless you wish to make a drive or similar online say via a tape cartridge.

     
  • Vincent (Bryan) Coen

    The last point don't make things more complex than needed i.e., stick to the KISS methodology.

     
    • cdg

      cdg - 2020-08-10

      "Keep it simple". I agree, which is why I wonder why you find all that code necessary, instead of the two lines of code (select and set) shown above that work for me in every Cobol program using a sort file.

       
  • Vincent (Bryan) Coen

    Because the program is multi platform.

     
    • Simon Sobisch

      Simon Sobisch - 2020-08-11

      I think the easiest way is to setup the file name via environment (outside of the COBOL program) in this case. This leaves all the setup external.

       
  • Gary Van Winkle

    Gary Van Winkle - 2020-08-12

    I'm trying to avoid adding elements outside the source file. I want the user to compile the program and run it. So I'm learning I need to take Linux/Unix into consideration, as well. I' m just an unwashed Windosian with 40+ yrs financial applications experience on mainframes, mostly. When somebody says "JCL" my mind freezes over.
    Anyhow, I've tried various approaches to the problem and it persists. If it's any help. I'm using GnuCOBOL 3.1-rel candidate on Windows-10. I'll try to attach my source file (.txt) in Landscape mode.

     
    • cdg

      cdg - 2020-08-12

      Once again, you have not provided enough information to solve your problem. I tried to compile your program, so I could see what was happening, and advise you how to fix the error. The program file you gave us has multiple errors that will prevent compilation, and is missing various needed data definitions.
      Once you make all the changes needed for a clean compile, I suggest that you put a display at (what is now) line 44, and you will probably discover that SORTWK does not contain what you think it does (assuming it even exists, because you haven't defined it). Furthermore, it is NOT the same as SORT-WK, which is what the file open routine will be looking for, according to the code you now have. (Note that, when you use "STRING", you have to clear the unused part of the data field first, because it will not be space filled. That may be you problem (besides a program that won't compile)).
      I'm not sure what you are trying to do. Simply say "SELECT SORT-FILE ASSIGN TO "SORTWK", and let the program create SORTWK in the default directory. (It's only a temporary file, so why worry abut the directory?) Or point the select statement to a working-storage variable, and create a proper path name. It isn't that complicated.

      How did you use mainframes for 40 years without using JCL (or batch or command files, as they are called in Windoze)?

       

      Last edit: cdg 2020-08-13
  • Gary Van Winkle

    Gary Van Winkle - 2020-08-13

    Yes, I had to use JCL on the mainframes. Yes, I had to use bat files in DOS. I don't remember much about either of them, frankly. It's a relief.
    Anyhow, I straightened myself out and I managed to get past the RELEASE problem. I followed Vincent's suggestion. That's a relief, too.
    Now I have a problem with the OUTPUT procedure. Looks like I'm getting back only the second half of the sorted file. When something like that happened on a mainframe it usually meant the record/blocking factors were wonky. My counters show me that all the records were RELEASED in the INPUT procedure.
    If I can't dope this out tomorrow I'll send the whole .cbl file and you can check out this weekend.
    And thanks to you both for your patience and your help.

     
  • Gary Van Winkle

    Gary Van Winkle - 2020-08-17

    Sorry to take so much time getting back to you. Family activity over the weekend, won't you know. Anyhow, all problems solved. Both methods of ASSIGNing file names (DISK or symbolic filename populated with drive:\path\filename.ext) are successful. The symbolic method would be useful when ACCEPTing an ASSIGNment from the user at runtime. Amazingly, I found an example of SORT in the Programmer's Guide. Can you find it? I've attached the source file. It's Free Format. Landscape format works best with Notepad. Thanks, everyone, for your patience and your priceless assistance. 'Til next time.

     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.