Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi File Processing - Merge 1

Status
Not open for further replies.

DarkByte

Programmer
Feb 22, 2012
10
US
I'm trying to design a Purge/Merge program that uses 4 input files and merges them into one output file. The issue is I want to keep track of all four files and which record I'm processing, without having to develop a lot of spegetti code. Has anyone designed such a neatly coded program?

 
Code:
01  WS-x-key.
88  x-AT-END Value High-Value.
  etc.


Perform Until A-AT-END and B-AT-END and C-AT-END and D-AT-END
  If A-KEY < B-KEY
    If A-KEY < c-KEY
      If A-KEY < D-KEY
        Merge-A
      Else
        Merge-D
      End-If
    Else
      If C-KEY < D-KEY
        Merge-C
      Else
        Merge-D
      End-If
   Else
  If B-KEY < c-KEY
    If B-KEY < D-KEY
      Merge-B
    Else
      Merge-D
    End-IF
  Else
    If C-KEY < D-KEY
       Merge-C
    Else
       Merge-D
    End-If
  End-If
End-Perform

Merge-x.
  Write from x
  Read x
    At End
       Move High-VALUE to WS-x-KEY
    Not At End
       MoVE x-FD-KEY   TO ws-x-key
       {Note:  This may actuallly be a series of moves if the key is complex.}
  End-Read
  .
 
You may want to look at this a little closer webrabbit.
At the beginning ....

If A-KEY < B-KEY
If A-KEY < c-KEY
If A-KEY < D-KEY
Merge-A
Else
Merge-D
End-If

You're making a big assumption in the ELSE. You don't know how D-KEY relates to B-KEY and C-KEY. All you know is that it is greater than A-KEY. Just because you know that A-KEY is less than B-KEY and C-KEY, that doesn't mean that D-KEY is also less than those two. The ELSE needs to verify that D-KEY is less than both B-KEY and C-KEY before you can merge D-KEY.

The code needs to be a little more complex than what you have written.
 
Thanks for your input webrabbit, the code you provided is close to what I'm see today, and I wanted to change that spigette code. The files are in sort order so that my take some of the guess work out it, the files have several different record types.
If file-1-ssn = file-2-ssn and = file-3
print the elig rec for all 3 files.


I need to know if there's a more modern approach then a bunch of IF statements, perhaps using the Evaluate to make it easier to read and maintain.
 
Well, there are actually only four conditions you need to test, but the tests are complex in COBOL. (Some other language may have a function to find the lowest of a set of strings.)
1) A<=B and A<=c and A<=D {A is not greater than any other}
2) B<A and B<=C and B<=D {B is not greater than any other}
3) c<A and C<B AND C<=D {C is not greater than any other}
4) other {D is the lowest}
To set this in an Evaluate statement, I would do it as follows:
Code:
Evaluate A<=B A<=C A<=D B<=C B<=D c<=D
  When True True True Any Any Any *> tesing A
    Process A
  When False Any Any True True Any *> testing B
    Process B
  When Any False Any False Any True *> testing C
    Process c
  When Other
    Process D
End Evalueate
Note that there are three True/False tests and three irrelevants (Any) on the first three When's.
 
webrabbit is showing some uncharacteristic syntactical problems, by omitting the ALSO keyword and a hyphen. There is also, perhaps, a better way to write this in an EVALUATE that I find more readable.

Code:
Evaluate TRUE also TRUE also TRUE
  When A <= B also A <= C also A <= D  *> testing A
    [i]Process A[/i]
  When A > B  also B <= C also B <= D  *> testing B
    [i]Process B[/i]
  When A > C  also B > C  also C <= D  *> testing C
    [i]Process C[/i] 
  When Other
    [i]Process D[/i]
End-Evaluate

(And, lest I make my own errors, 'typed, not tested!')

Tom Morrison
Micro Focus
 
Yes, what I put in the 2nd time was intended to be a guideline, not actual code. Tom, your answer is much more elegant than mine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top