This exchange showed up on the PDS echo not so very long ago. The
author of the code is Donn Bly, the moderator of that echo. He was
answering a message from Bob Ross. I thought it was good enough to save
and to share in this echo. It's a common problem we all run up against
eventually - how to merge several sorts into one big sort.
======================== START QUOTE ================================
BR> I'm trying to figure out how to merge two sorted text
BR> files into one sorted output file without much luck.
> What you want to do is called "transaction processing". In the old days we
> would have one or more sorted transaction files and a sorted master file.
> We would read the transaction files, then find and update the proper record
> in the master file, leaving the unchanged records in the master file
> intact. Since the files would be located on tape, you had to read them
> sequentially. Oh well, I digress. You might try something like this:
'----------------------- START CODE ---------------------------
OPEN "F1" FOR INPUT AS #1
OPEN "F2" FOR INPUT AS #2
OPEN "F3" FOR OUTPUT AS #3
EFlag1% = 0
Eflag2% = 0
IF EOF(1) THEN EFlag1% = -1 ELSE LINE INPUT #1, Buff1$
IF EOF(2) THEN Eflag2% = -1 ELSE LINE INPUT #2, Buff2$
DO UNTIL EFlag1% AND Eflag2%
IF EFlag1% THEN
NextRec% = 2
ELSEIF Eflag2% THEN
NextRec% = 1
ELSEIF Buff1$ <= Buff2$ THEN
NextRec% = 1
ELSE
NextRec% = 2
END IF
IF NextRec% = 1 THEN
PRINT #3, Buff1$
PRINT Buff1$
IF EOF(1) THEN EFlag1% = -1 ELSE LINE INPUT #1, Buff1$
ELSE
PRINT #3, Buff2$
PRINT Buff2$
IF EOF(2) THEN Eflag2% = -1 ELSE LINE INPUT #2, Buff2$
END IF
LOOP
CLOSE #1, #2, #3
'------------------------------ END CODE ----------------------
* SLMR 2.1a * MAXLIB For PB v1.1 - Access arrays and files in EMS/XMS!
--- WILDMAIL!/WC v4.12
---------------
* Origin: Com-Dat BBS - Hillsboro, OR. HST DS (1:105/314.0)
|