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. Advertising

  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. Advertising

  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. Advertising

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:
    736
  2. MacDk
    Replies:
    0
    Views:
    427
    MacDk
    Jul 31, 2003
  3. Steven Woody
    Replies:
    2
    Views:
    322
    Jay Nabonne
    Dec 29, 2005
  4. Mathew Cucuzella

    using TkEntry with copy/paste

    Mathew Cucuzella, Jun 7, 2010, in forum: Ruby
    Replies:
    4
    Views:
    80
    Mathew Cucuzella
    Jun 8, 2010
  5. Replies:
    1
    Views:
    312
Loading...

Share This Page