Forum: Ruby basic question...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F9cea624b1da9da8c422da221cdcedcf?d=identicon&s=25 unknown (Guest)
on 2006-03-22 16:59
(Received via mailing list)
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.
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2006-03-22 17:03
(Received via mailing list)
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
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-03-22 17:04
(Received via mailing list)
<kidding>This is a Ruby and not a Basic group.</kidding>

mopthisandthat@hotmail.com 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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-03-22 17:16
(Received via mailing list)
On Mar 22, 2006, at 9:58 AM, mopthisandthat@hotmail.com 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
This topic is locked and can not be replied to.