Stream merge: Difference between revisions

→‎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.
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:
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).
 
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; ···'''
Line 1,902 ⟶ 1,904:
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 */
@.=copies('ff'x, 1e51e4); 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.*/
n=n-1 /*adj.fix N; read'cause fromread a non─existent file*/
do forever; y=@.; #=0 /*find the lowest value for N values.*/
do k=1 for n; if @.k==@. then call rdr k /*traipseNot throughdefined? the stemmedThen read @a array.file record*/
if @.k==@.<<y then do; y=@.k; call rdr #=k; end /*NotLowest definedso far? ThenMark readthis aas file recordminimum.*/
end if @. /*k<<y*/ then do; y=@.k; #=k; end /*Lowest so[↑] far? Thennote markuse thisof << as(exact min.compare)*/
end /*k*/ /* [↑] note use of << exact comparison*/
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 rdr # /*re-populatere─populate a value from the # file. */
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>
{{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>