in reply to parsing CSV file with embedded commas (fortunately, fixed-width) - is unpack the solution?

unpack is a good answer as long as it's a Fixed Width format file and nothing else seems to work quite right since its not quite actual CSV. You might also want to try Parse::FixedLength, it might go something like this:
use Parse::FixedLength: # Set field names and lengths # (you might want to have more meaningful names) :-) my $parser = Parse::FixedLength->new([ field1=>8, field2=>10, ...etc. ]); my @names = @{$parser->names}; while (<>) { my $data = $parser->parse($data); # Strip leading and trailing quotes and spaces? s/^'?\s+|\s+'?,$//g for values %$data; # Output real csv, e.g. s/"/""/g for values %$data; print join(",", map qq("$_"), @$data{@names}),"\n"; }
  • Comment on Re: parsing CSV file with embedded commas (fortunately, fixed-width) - is unpack the solution?
  • Download Code

Replies are listed 'Best First'.
Re: Re: parsing CSV file with embedded commas (fortunately, fixed-width) - is unpack the solution?
by bh_perl (Monk) on Jul 14, 2003 at 03:44 UTC
    Hi, I have tried your new method but it's some error message here : Can't locate object method "new" via package "Parse::FixLength" (perhaps you forgot to load "Parse::FixLength"?) at ./scal.pl line 18. Do you know what that's mean ?
      Can't locate object method "new" via package "Parse::FixLength"
      Did you install the module? If you installed it, you must have spelled it correctly once ('Parsed::FixedLength'). You just need to spell it correctly again.