in reply to parsing CSV file with embedded commas (fortunately, fixed-width) - is unpack the solution?
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"; }
|
|---|
| 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 | |
by runrig (Abbot) on Jul 14, 2003 at 17:38 UTC |