implement "paste" using ruby

Discussion in 'Ruby' started by Oliver, Mar 4, 2009.

  1. Oliver

    Oliver Guest

    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
     
    Oliver, Mar 4, 2009
    #1
    1. Advertisements

  2. 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-forum.com/.
     
    Brian Candler, Mar 4, 2009
    #2
    1. Advertisements

  3. Oliver

    Ryan Davis Guest

    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
     
    Ryan Davis, Mar 4, 2009
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    859
  2. Steven Woody
    Replies:
    2
    Views:
    417
    Jay Nabonne
    Dec 29, 2005
  3. mark
    Replies:
    22
    Views:
    936
    Jonathan Gardner
    Jan 9, 2009
  4. Ant Sims
    Replies:
    5
    Views:
    260
    Robert Klemme
    Apr 4, 2005
  5. anne001
    Replies:
    1
    Views:
    948
  6. George  Moschovitis

    Using ruby-openid/ruby-yadis w/o ruby gems

    George Moschovitis, Mar 10, 2007, in forum: Ruby
    Replies:
    1
    Views:
    218
    George Moschovitis
    Mar 11, 2007
  7. Mathew Cucuzella

    using TkEntry with copy/paste

    Mathew Cucuzella, Jun 7, 2010, in forum: Ruby
    Replies:
    4
    Views:
    147
    Mathew Cucuzella
    Jun 8, 2010
  8. Replies:
    1
    Views:
    484
Loading...