Stream merge: Difference between revisions
Content added Content deleted
m (→version 2: added text to the output section header comment about the name of the output file is different than version 1.) |
(→version 2: changed program so that it can run under PC/REXX and Personal REXX, also insured that running a 2nd time doesn't corrupt the output file.) |
||
Line 1,897: | Line 1,897: | ||
This REXX version reads (in numerical order) ''any'' number of input files in the form of: <big> nnn.TXT </big> |
This REXX version reads (in numerical order) ''any'' number of input files in the form of: <big> nnn.TXT </big> |
||
<br>and stops reading subsequent ''new'' input files when it encounters an input file that doesn't exist (or is empty). |
<br>and stops reading subsequent ''new'' input files when it encounters an input file that doesn't exist (or is empty). |
||
This REXX program will execute correctly when executed multiple times. |
|||
The input files would/should be named: '''1.TXT 2.TXT 3.TXT 4.TXT ···''' |
The input files would/should be named: '''1.TXT 2.TXT 3.TXT 4.TXT ···''' |
||
Line 1,902: | Line 1,904: | ||
No ''heap'' is needed to keep track of which record was written, nor needs replenishing from its input file. |
No ''heap'' is needed to keep track of which record was written, nor needs replenishing from its input file. |
||
<lang rexx>/*REXX pgm reads sorted files (1.TXT, 2.TXT, ···), and writes sorted data ───► ALL.TXT */ |
<lang rexx>/*REXX pgm reads sorted files (1.TXT, 2.TXT, ···), and writes sorted data ───► ALL.TXT */ |
||
@.=copies('ff'x, |
@.=copies('ff'x, 1e4); call lineout 'ALL.TXT',,1 /*no value should be larger than this. */ |
||
do n=1 until @.n==@.; |
do n=1 until @.n==@.; call rdr n; end /*read any number of appropriate files.*/ |
||
n=n-1 /* |
n=n-1 /*fix N 'cause read a non─existent file*/ |
||
do forever; |
do forever; y=@.; #=0 /*find the lowest value for N values.*/ |
||
do k=1 for n; if @.k==@. then call rdr k /*Not defined? Then read a file record*/ |
|||
if @.k<<y then do; y=@.k; #=k; end /*Lowest so far? Mark this as minimum.*/ |
|||
end /*k*/ /* [↑] note use of << (exact compare)*/ |
|||
end /*k*/ /* [↑] note use of << exact comparison*/ |
|||
if #==0 then exit /*stick a fork in it, we're all done. */ |
if #==0 then exit /*stick a fork in it, we're all done. */ |
||
call lineout 'ALL.TXT', @.#; |
call lineout 'ALL.TXT', @.#; say @.# /*write value to a file; also display.*/ |
||
call rdr # /* |
call rdr # /*re─populate a value from the # file. */ |
||
end /*forever*/ /*keep reading/merging until exhausted.*/ |
end /*forever*/ /*keep reading/merging until exhausted.*/ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
rdr: |
rdr: arg z; @.z= @.; f= z'.TXT'; if lines(f)\==0 then @.z= linein(f); return</lang> |
||
{{out|output|text= is the same as the 1<sup>st</sup> REXX version when using identical input files, except the output file is named '''ALL.TXT'''}} <br><br> |
{{out|output|text= is the same as the 1<sup>st</sup> REXX version when using identical input files, except the output file is named '''ALL.TXT'''}} <br><br> |
||