implement "paste" using ruby

O

Oliver

hi, all

I have a programming question: in the *NIX world, there is a small
utility named "paste", that can combine several files together by
columns. For example:

file "x.dat"'s content is:
1
2
3
....

file "y.dat"'s content is:
a
b
c
....

then "paste x.dat y.dat > z.dat" will generate z.dat as:
1 a
2 b
3 c
....

If I want to do it in Ruby, and number of files is a variable, and
each file itself can be potentially huge ... what would be the most
cost efficient way of implementing this?

Thanks in advance.

Oliver
 
B

Brian Candler

Oliver said:
If I want to do it in Ruby, and number of files is a variable, and
each file itself can be potentially huge ... what would be the most
cost efficient way of implementing this?

Assuming the number of files to paste together is reasonable (say under
1000), then I'd simply open all the files up front:

files = ARGV.map { |fname| File.open(fname) }

and then inside a loop use 'gets' to pick one line from each, and output
those values together.

HTH,

Brian.
 
R

Ryan Davis

If I want to do it in Ruby, and number of files is a variable, and
each file itself can be potentially huge ... what would be the most
cost efficient way of implementing this?

The most cost efficient way is by not reinventing the wheel:

system "paste", *ARGV
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top