Reputation: 21
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
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
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
Reputation: 10553
Two possible processes
The exact sequence would depend on the file output order. You could
merge
the 2 filesAlternatively you could
Sort Merge
file-1 and file-2 on ID-USER and create file-4 Sort Merge
file-1 and file-4 on Job-id Sort Merge
file-1 and file-2 on ID-USER and do an index-lookup on File-3iUpvotes: 3