Stream merge: Difference between revisions

Added Ruby
(Added Elixir)
(Added Ruby)
Line 651:
rdr: parse arg z; @.z=$; f=z'.TXT'; if lines(f)\==0 then @.z=linein(f); return</lang>
'''output''' &nbsp; is the same as the 1<sup>st</sup> REXX version when using identical input files). <br><br>
 
=={{header|Ruby}}==
<lang ruby>def stream_merge(*files)
fio = files.map{|fname| open(fname)}
merge(fio.map{|io| [io, io.gets]})
end
 
def merge(fdata)
until fdata.empty?
io, min = fdata.min_by{|_,data| data}
puts min
if (next_data = io.gets).nil?
io.close
fdata.delete([io, min])
else
i = fdata.index{|x,_| x == io}
fdata[i] = [io, next_data]
end
end
end
 
files = %w(temp1.dat temp2.dat temp3.dat)
files.each do |fname|
data = IO.read(fname).gsub("\n", " ")
puts "#{fname}: #{data}"
end
stream_merge(*files)</lang>
 
{{out}}
<pre>
temp1.dat: 1 3 9 14 15 17 28
temp2.dat: 7 8 14 14 23 26 28 29 30
temp3.dat: 9 23 25 29
1
3
7
8
9
9
14
14
14
15
17
23
23
25
26
28
28
29
29
30
</pre>
 
== {{header|UNIX Shell}} ==
Anonymous user