basic question...

Discussion in 'Ruby' started by mopthisandthat@hotmail.com, Mar 22, 2006.

  1. Guest

    Hello,

    Suppose "OOO.csv" is a data file with 9 columns, and I want to create a
    new file that is consisted of 2nd, 5th, and 7th columns of the OOO.csv
    (i.e., subset of the original), how do I do this?

    Thanks.
     
    , Mar 22, 2006
    #1
    1. Advertising

  2. <kidding>This is a Ruby and not a Basic group.</kidding>

    wrote:
    > Suppose "OOO.csv" is a data file with 9 columns, and I want to create a
    > new file that is consisted of 2nd, 5th, and 7th columns of the OOO.csv
    > (i.e., subset of the original), how do I do this?


    Use CSV for parsing and writing:

    http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html

    Kind regards

    robert
     
    Robert Klemme, Mar 22, 2006
    #2
    1. Advertising

  3. Alder Green Guest

    Hi

    Iterate over OOO.csv's lines, split each line as you wish (probably
    using slice notation is the easiest for fixed length columns), and
    writing each slice into the desired destination file.

    Regards,
    Alder

    On 3/22/06, <> wrote:
    > Hello,
    >
    > Suppose "OOO.csv" is a data file with 9 columns, and I want to create a
    > new file that is consisted of 2nd, 5th, and 7th columns of the OOO.csv
    > (i.e., subset of the original), how do I do this?
    >
    > Thanks.
    >
    >
    >
     
    Alder Green, Mar 22, 2006
    #3
  4. On Mar 22, 2006, at 9:58 AM, wrote:

    > Hello,
    >
    > Suppose "OOO.csv" is a data file with 9 columns, and I want to
    > create a
    > new file that is consisted of 2nd, 5th, and 7th columns of the OOO.csv
    > (i.e., subset of the original), how do I do this?


    My suggestion, if you don't mind using a non-standard library:

    Neo:~/Desktop$ ls
    000.csv trim_columns.rb
    Neo:~/Desktop$ cat 000.csv
    1,2,3,4,5,6,7,8,9
    a,b,c,d,e,f,g,h,i
    Neo:~/Desktop$ ruby -rubygems trim_columns.rb 000.csv
    2,5,7
    b,e,g
    Neo:~/Desktop$ cat trim_columns.rb
    #!/usr/local/bin/ruby -w

    require "faster_csv"

    FasterCSV.filter { |row| row.replace(row.values_at(1, 4, 6)) }

    __END__

    Hope that helps.

    James Edward Gray II
     
    James Edward Gray II, Mar 22, 2006
    #4
    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. Stefan Duenser

    Basic shifting question

    Stefan Duenser, Dec 7, 2004, in forum: VHDL
    Replies:
    4
    Views:
    463
    Stefan Duenser
    Dec 8, 2004
  2. Replies:
    1
    Views:
    602
    Eric Smith
    Jul 8, 2005
  3. Engineer
    Replies:
    6
    Views:
    685
    Jeremy Bowers
    May 1, 2005
  4. Replies:
    0
    Views:
    483
  5. Ark Khasin

    A basic question question about volatile use

    Ark Khasin, Jul 31, 2008, in forum: C Programming
    Replies:
    12
    Views:
    381
    Ark Khasin
    Jul 31, 2008
Loading...

Share This Page