Jump to content

Stream merge: Difference between revisions

m
→‎version 2: elided the use of a dummy variable, added/changed comments and whitespace, utilized the number of files without fudging, used a template for OUTPUT, added verbiage to the REXX section header.
(Added Sidef)
m (→‎version 2: elided the use of a dummy variable, added/changed comments and whitespace, utilized the number of files without fudging, used a template for OUTPUT, added verbiage to the REXX section header.)
Line 1,266:
 
===version 2===
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; and
<br>and stops reading subsequent &nbsp; ''new'' &nbsp; input files when it encounters aan input file that doesn't exist &nbsp; (or is empty).
 
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; ···'''
 
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('FFff'x, 1e5) /*no value should be larger than this. */
@.=$ do n=1 until @.n==@.; call rdr n; end /*theread defaultany valuenumber forof theappropriate @ arrayfiles. */
n=n-1 do n=1 until @.n==$; call rdr n; end /*readadj. anyN; numberread offrom appropriatea files.non─existent file*/
do forever; y=@.; #=0 /*find [↑]the lowest readvalue 'tilfor a non─existentN file. values.*/
do k=1 forever; for n y=$; #=0 /*find the lowest value for /*traipse through the stemmed N @ valuesarray.*/
if do @.k=1 for n-1 =@. 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 defined?so far? Then readmark athis fileas recordmin.*/
end if @./*k<<y*/ then do; y=@.k; #=k; end /*Lowest so[↑] far? note Thenuse markof this<< asexact mincomparison*/
end /*k*/
if #==0 then exit /*stick a fork in it, we're all done. */
call lineout 'ALL.TXT', @.#; say @.# /*outputwrite value to a file; also display. */
call rdr # /*repopulatere-populate thisa file'svalue inputfrom value.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).}} <br><br>
 
=={{header|Ruby}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.