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 &nbsp; (in numerical order) &nbsp; ''any'' &nbsp; number of input files in the form of: &nbsp; &nbsp; <big> nnn.TXT </big> &nbsp; &nbsp;
This REXX version reads &nbsp; (in numerical order) &nbsp; ''any'' &nbsp; number of input files in the form of: &nbsp; &nbsp; <big> nnn.TXT </big> &nbsp; &nbsp;
<br>and stops reading subsequent &nbsp; ''new'' &nbsp; input files when it encounters an input file that doesn't exist &nbsp; (or is empty).
<br>and stops reading subsequent &nbsp; ''new'' &nbsp; input files when it encounters an input file that doesn't exist &nbsp; (or is empty).

This REXX program will execute correctly when executed multiple times.


The input files would/should be named: &nbsp; &nbsp; '''1.TXT &nbsp; &nbsp; 2.TXT &nbsp; &nbsp; 3.TXT &nbsp; &nbsp; 4.TXT &nbsp; &nbsp; ···'''
The input files would/should be named: &nbsp; &nbsp; '''1.TXT &nbsp; &nbsp; 2.TXT &nbsp; &nbsp; 3.TXT &nbsp; &nbsp; 4.TXT &nbsp; &nbsp; ···'''
Line 1,902: Line 1,904:
No &nbsp; ''heap'' &nbsp; is needed to keep track of which record was written, nor needs replenishing from its input file.
No &nbsp; ''heap'' &nbsp; 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, 1e5) /*no value should be larger than this. */
@.=copies('ff'x, 1e4); call lineout 'ALL.TXT',,1 /*no value should be larger than this. */
do n=1 until @.n==@.; call rdr n; end /*read any number of appropriate files.*/
do n=1 until @.n==@.; call rdr n; end /*read any number of appropriate files.*/
n=n-1 /*adj. N; read from a non─existent file*/
n=n-1 /*fix N 'cause read a non─existent file*/
do forever; y=@.; #=0 /*find the lowest value for N values.*/
do forever; y=@.; #=0 /*find the lowest value for N values.*/
do k=1 for n /*traipse through the stemmed @ array.*/
do k=1 for n; if @.k==@. then call rdr k /*Not defined? Then read a file record*/
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.*/
if @.k<<y then do; y=@.k; #=k; end /*Lowest so far? Then mark this as min.*/
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', @.#; say @.# /*write value to a file; also display. */
call lineout 'ALL.TXT', @.#; say @.# /*write value to a file; also display.*/
call rdr # /*re-populate a value from the # file. */
call rdr # /*re─populate a value from the # file. */
end /*forever*/ /*keep reading/merging until exhausted.*/
end /*forever*/ /*keep reading/merging until exhausted.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
rdr: parse arg z; @.z=@.; f=z'.TXT'; if lines(f)\==0 then @.z=linein(f); return</lang>
rdr: arg z; @.z= @.; f= z'.TXT'; if lines(f)\==0 then @.z= linein(f); return</lang>
{{out|output|text=&nbsp; is the same as the 1<sup>st</sup> REXX version when using identical input files, &nbsp; except the output file is named &nbsp; '''ALL.TXT'''}} <br><br>
{{out|output|text=&nbsp; is the same as the 1<sup>st</sup> REXX version when using identical input files, &nbsp; except the output file is named &nbsp; '''ALL.TXT'''}} <br><br>