I can't get your implementation to work . . .
 
What about s/^\s+|\s+$//g ? shouldn't it be s/(^\s+)|(\s+$)//g ?
 
And why the if in:
 
$_ = "\"$_\"" if ( $_ =~ /[,"]/ );
 
aren't you trying to keep that trailing comma off - I don't see where you
add the comma.
 
Tim
______________________________________________________________________
 
you said . . .
 
I would do something like this. ( I am not sure how efficient is this though
).
 
open( INFILE, 'in.txt' ) or die;
open( OUTFILE, '>out.txt' ) or die;
 
while ( <INFILE> )
{
    s/^\s+|\s+$//g;  # Eat leading & trailing spaces
    next unless $_;  # Skip empty lines
 
    my @fields = split /\s+/;
    
    foreach (@fields)
    {
        s/"/""/g;
        $_ = "\"$_\"" if ( $_ =~ /[,"]/ );
    }
 
    print OUTFILE join( ',', @fields ), "\n";
}
 
close INFILE;
close OUTFILE;
  


Reply via email to