The new Text::CSV_XS has callbacks for that:

my $aoh = csv (in => $xFile[$k], headers => "auto", callbacks => { after_parse => sub { $_ ||= 0 for @{$_[1]} }});

As an example:

$ cat test.pl #!/pro/bin/perl use 5.16.2; use warnings; use Text::CSV_XS qw(csv); use Data::Peek; DDumper (csv (in => *DATA, headers => "auto", callbacks => { after_parse => sub { $_ ||= 0 for @{$_[1]} }})); __END__ a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I, +J,K 11004516,0,0,9,9,3,12477,,,4,,0,,,3,38a947a1,b66b7850,6a14f9b9 1100699 +5,1,,-1,,,,, $ perl test.pl [ { A => 0, B => 0, C => 0, D => 0, E => 0, F => 0, G => 'fbc55dae', H => '9a89b36c', I => '58e67aaf', J => 'f600ec0b', K => 0, a => 11004516, b => 0, c => 0, d => 9, e => 9, f => 3, g => 12477, h => 0, i => 0, j => 4, k => 0, l => 0, m => 0, n => 0, o => 3, p => '38a947a1', q => 'b66b7850', r => '6a14f9b9 11006995', s => 1, t => 0, u => -1, v => 0, w => 0, x => 0, y => 0, z => 0 } ]

update: as Parse::CSV uses Text::CSV_XS underneath, you can just add the callback to your code if you have a recent enough version of Text::CSV_XS.

my $csv = Parse::CSV->new ( file => $xFile[$k], header => "auto", names => 1, empty_is_undef => 1, auto_diag => 1, callbacks => { after_parse => sub { $_ ||= 0 for @{$_[1] } }, ));


Enjoy, Have FUN! H.Merijn

In reply to Re: csv parsing with multiple missing values/multiple commas by Tux
in thread csv parsing with multiple missing values/multiple commas by f77coder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.