Would somebody show me how to do it in Ruby? I have used Perl before
for data parsing, but have forgotten most of the usage. I am interested
in Ruby, wishing it’s easier to learn and maintain than Perl.
Would somebody show me how to do it in Ruby? I have used Perl before
for data parsing, but have forgotten most of the usage. I am interested
in Ruby, wishing it’s easier to learn and maintain than Perl.
Thanks.
Dong
How about this,
fname = nil
File.open(“orig”) {|f|
while(l = f.gets)
if l =~ /^Part ([A-Z])/
file.close if fname
fname = $1
file = File.open(fname,“w”)
end
file.puts l if fname
end
}
file.close if fname
Shin Nishiyama shin@xtal.tf.chiba-u.ac.jp --------
Department of Materials Technology,
Faculty of Engineering, Chiba-University
On Fri, Oct 18, 2002 at 08:27:58AM +0900, Dong wrote:
Would somebody show me how to do it in Ruby? I have used Perl before
for data parsing, but have forgotten most of the usage. I am interested
in Ruby, wishing it’s easier to learn and maintain than Perl.
Thanks for all who replied. Ruby is quite impressive. I am reading
the online Programming Ruby and the turorial at Rubycentral. My use
of Ruby for now would be mostly data parsing, nothing advanced.
Anybody have pointers to other sources?
Thanks for all who replied. Ruby is quite impressive. I am reading
the online Programming Ruby and the turorial at Rubycentral. My use
of Ruby for now would be mostly data parsing, nothing advanced.
Anybody have pointers to other sources?
“The Ruby Way” (dead tree book) has a lot of “how to do this” kind of
information. Very helpful.
By reading the file directly? Yes, you’re not missing anything. I
just like not to care whence the information comes from, be it a file
or a socket or a command’s output or whatever; the proposed form won’t
care about it either, and will just shut up and work.
Plus I love awards. (See Stefan’s link.)
Massimiliano
···
On Fri, Oct 18, 2002 at 11:59:11PM +0900, Michael Campbell wrote:
$ cat file | ruby -e ‘STDIN.read.split(/\n\n/).each_with_index
{|part,i| File.open(“f.#{i}”, “w”).puts(part) }’
You can save a few CPU cycles by removing cat there, no? Or am I
missing something subtle.
Or you just meant ruby -e … <file? Yes again (I don’t know if that
works with every shell, though).
Massimiliano
···
On Fri, Oct 18, 2002 at 11:59:11PM +0900, Michael Campbell wrote:
$ cat file | ruby -e ‘STDIN.read.split(/\n\n/).each_with_index
{|part,i| File.open(“f.#{i}”, “w”).puts(part) }’
You can save a few CPU cycles by removing cat there, no? Or am I
missing something subtle.