[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

implement "paste" using ruby

Oliver

3/4/2009 4:29:00 AM

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
5 Answers

Brian Candler

3/4/2009 9:04:00 AM

0

Oliver wrote:
> 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.
--
Posted via http://www.ruby-....

Ryan Davis

3/4/2009 10:13:00 AM

0


On Mar 3, 2009, at 20:28 , Oliver wrote:

> 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


Yossef Mendelssohn

3/4/2009 4:22:00 PM

0

On Mar 4, 4:12=A0am, Ryan Davis <ryand-r...@zenspider.com> wrote:
> The most cost efficient way is by not reinventing the wheel:
>
> =A0 =A0system "paste", *ARGV

To me, the reasoning behind the question seemed to be what to do if
the sytem has Ruby installed, but not the 'paste' utility. Or maybe
it's simply an exercise in handling memory usage, speed, and overall
efficiency.

Either way, this answer is not nearly as helpful as you seem to think
it is.

But that's just what I think. Maybe Oliver has different ideas.

--
-yossef

Rick DeNatale

3/4/2009 5:33:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Tue, Mar 3, 2009 at 11:28 PM, Oliver <fwang2@gmail.com> wrote:

> 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
> ...
>
>
#!/usr/bin/env ruby -wKU

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

while (lines = files.map {|file| file.gets}).any? {|line| line}
puts lines.map {|line| line.to_s.chomp}.join("\t")
end





--
Rick DeNatale

Blog: http://talklikeaduck.denh...
Twitter: http://twitter.com/Ri...
WWR: http://www.workingwithrails.com/person/9021-ric...
LinkedIn: http://www.linkedin.com/in/ri...

Ryan Davis

3/4/2009 7:41:00 PM

0


On Mar 4, 2009, at 08:22 , Yossef Mendelssohn wrote:

> On Mar 4, 4:12 am, Ryan Davis <ryand-r...@zenspider.com> wrote:
>> The most cost efficient way is by not reinventing the wheel:
>>
>> system "paste", *ARGV
>
> To me, the reasoning behind the question seemed to be what to do if
> the sytem has Ruby installed, but not the 'paste' utility. Or maybe
> it's simply an exercise in handling memory usage, speed, and overall
> efficiency.
>
> Either way, this answer is not nearly as helpful as you seem to think
> it is.

I disagree. The OP emphasized the _most_ cost efficient way. In terms
of both CPU time _and_ developer time efficiency, my method reigns
supreme. I learned this valuable lesson while at Gemstone. They used
unix sort for certain types of sorts (esp for large data) because you
can't beat a 20+ year old tool, so why reinvent the wheel?