Robson de Melo
Robson de Melo

Reputation: 21

How to do balance line in Cobol using 3 input files?

my challenge on class is code a program which read 3 input txt files and generate 1 txt output file. All input files must be preferentially line sequential mode and no one indexed (this are rules for breaking our minds).

File 1 has field ID-USER (ordered by it) and others; File 2 has fiels ID-USER (ordered by it), ID-JOB and others; File 3 has field ID-JOB (ordered by it) and others. Output file will join data from 3 files.

Balance line in cobol with 2 files is a hard work but I can do it. But using 3 input files, when ID-USER is corresponding in files 1 and 2, I must advance read on file 3 to record corresponding ID-JOB on files 2 and 3, and my balance line is doesn't more work because I don't know if is possible "restart" read on file 3.

Resuming: how to restart a read using sequential mode on specific input file (file 3)? Or reverse search direction on same cobol program.

Upvotes: 2

Views: 1359

Answers (3)

Brian Tiffin
Brian Tiffin

Reputation: 4126

I'm never quite sure on Stack Overflow and home work when it comes to referring to online resources, but the GnuCOBOL FAQ has a sample of merging line sequential files.

Hopefully the little sample still leaves you with a chance to learn about MERGE and not rob you of any opportunity.

https://open-cobol.sourceforge.io/faq/index.html#merge

Note in the syntax rail diagram how ON ... KEY phrases can be repeated per file, with multiple files.

To avoid link rot; here is the code, but this should always be findable by web searching for "GnuCOBOL FAQ" in the MERGE reserved word entry.

GCobol >>SOURCE FORMAT IS FIXED
      *> ***************************************************************
      *> Author:    Brian Tiffin
      *> Date:      20140610
      *> Purpose:   Demonstrate a merge pass
      *> Tectonics: cobc -x gnucobol-merge-sample.cob
      *> ***************************************************************
       identification division.
       program-id. gnucobol-merge-sample.

       environment division.
       configuration section.
       repository.
           function all intrinsic.

io     input-output section.
       file-control.
           select master-file
               assign to "master-sample.dat"
               organization is line sequential.

           select eastern-transaction-file
               assign to "east-transact-sample.dat"
               organization is line sequential.

           select western-transaction-file
               assign to "west-transact-sample.dat"
               organization is line sequential.
           select merged-transactions
               assign to "merged-transactions.dat"
               organization is line sequential.

           select working-merge
               assign to "merge.tmp".

data   data division.
       file section.
       fd master-file.
          01 master-record     pic x(64).

       fd eastern-transaction-file.
          01 transact-rec      pic x(64).

       fd western-transaction-file.
          01 transact-rec      pic x(64).

       fd merged-transactions.
          01 new-rec           pic x(64).

       sd working-merge.
          01 merge-rec.
             02 master-key     pic 9(8).
             02 filler         pic x.
             02 action         pic xxx.
             02 filler         PIC x(52).

code  *> ***************************************************************
      *> not much code
      *>     trick.  DEP, CHQ, BAL are action keywords.  They sort
      *>     descending as DEP, CHQ, BAL, so do all deposits, then
      *>     all withdrawals, then balance reports.
      *> ***************************************************************
       procedure division.
       merge working-merge
           on ascending key master-key
              descending key action
           using eastern-transaction-file,  western-transaction-file,
                 master-file
           giving merged-transactions
done   goback.
       end program gnucobol-merge-sample.

Data samples look like

11111111 CHQ 0001111.11 withdrawal from account one
33333333 DEP 0333333.33 third of a million in, pocket change
33333333 CHQ 0000333.33 payroll
33333333 CHQ 0000333.33 payroll
33333333 CHQ 0000333.33 payroll
55555555 DEP 0000555.55 deposit to new record five
55555555 CHQ 0000055.55 withdrawal from account five

East

11111111 CHQ 0001111.11 withdrawal from account one
44444444 DEP 0000044.44 deposit to account four
66666666 BAL balance request for account six

West, etc.

GnuCOBOL makes it pretty easy to deal with the LINE SEQUENTIAL part.

There are more questions in your question, not mentioned here, as this listing is just to demonstrate MERGE with LINE SEQUENTIAL, and to not worry about head explosions.

Upvotes: 3

Rick Smith
Rick Smith

Reputation: 4417

How to restart a read using sequential mode on specific input file (file 3)?

To restart the reading of a file from the beginning, use CLOSE then OPEN INPUT.

Or reverse search direction on same cobol program.

There are no COBOL statements to read a line sequential file in reverse. It is possible by calling a C program to scan File 3 in reverse by treating it as a "binary" file.

It is not clear to me the term, balance line, should apply in this case. This is because the third file does not share the same sequence as the second file.

Bruce Martin has provided some suggestions for accomplishing the task.

This being a "challenge [in] class", it may be a case where you cannot use those suggestions.

However, if you want to proceed as you have outlined, then you need to be aware that if you "restart" when ID-JOB of File 2 is less than ID-JOB of File 3, you need to treat that as a potential edge case. Specifically, when ID-JOB of File 2 is less than the minimum value of ID-JOB of File 3, an infinite loop is possible.

Upvotes: 2

Bruce Martin
Bruce Martin

Reputation: 10553

Two possible processes

  1. 2 sort merge programs one on Job-Id, one on ID-USER
  2. Load File-3 into a indexed file (VSAM file on the mainframe) or a data base. The key for the Index-File/Db would be Job-Id. Then you can do a indexed read

Two Programs

The exact sequence would depend on the file output order. You could

  1. Sort File-2 and File-3 on Job-id and create an output file-4 with the required data from both files
  2. Sort file-1 and file-4 on ID-USER and merge the 2 files

Alternatively you could

  1. Sort Merge file-1 and file-2 on ID-USER and create file-4
  2. Sort Merge file-1 and file-4 on Job-id

Index file solution

  1. Load file-3 into an index file (say file-3i) (could use an array if small) either before your program starts or as part of the initialization.
  2. Sort Merge file-1 and file-2 on ID-USER and do an index-lookup on File-3i

Upvotes: 3

Related Questions