Hi, I'm trying to convert a Perl regex script to Ruby and I'm not sure how to 
do one little thing..

I have a csv file that has newline characters (^M aka \r\n) in the middle of 
the lines.  My Perl script that fixes this is:

foreach $file (@ARGV) {
    open(IN,"$file") or die "can't open $file : $!";
    @f=<IN>;
    $f=join("",@f);
    $f=~s/[\r\n](?!BOB|JOE)//mg;
    open(OUT,">$file") or die "can't open $file for writing : $!";
    print OUT $f;
}

Where BOB and JOE are the only two strings that are expected to start a line.  
Basically it's checking for a newline that isn't followed by either BOB or JOE 
and deleting them.  I'm sure there are other ways to accomplish this, but it 
works.  So now I'm trying to do this in Ruby, and have the following code so 
far:

ARGV.each do |filename|
  open(filename, "r+") do |f|
    lines = f.readlines
    lines.each do |it|
      it.gsub!(/[\r\n](BOB|JOE)/m,'')
    end
    f.pos = 0
    f.print lines
    f.truncate(f.pos)
  end
end

And of course I've tried some variations of it.  I'm just not sure what to use 
for the lookahead that the "(?" accomplishes in the Perl script.

Thanks for any help.
Bryan




_______________________________________________
PDXRuby mailing list
[email protected]
IRC: #pdx.rb on irc.freenode.net
http://lists.pdxruby.org/mailman/listinfo/pdxruby

Reply via email to