Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: csv parsing with multiple missing values/multiple commas

by poj (Priest)
on Aug 02, 2014 at 18:47 UTC ( #1096026=note: print w/ replies, xml ) Need Help??


in reply to Re^2: csv parsing with multiple missing values/multiple commas
in thread csv parsing with multiple missing values/multiple commas

Try this ;

#!perl use 5.12.0; use warnings; use strict; use File::Basename; use Text::CSV_XS; use Benchmark; my $start = time; my $t0 = new Benchmark; print "\n Current Date and Time -> " . localtime() . "\n"; my $Base = 'c:/temp'; my $s_DIR=$Base.''; my $p_DIR=$Base.''; my $f_DIR=$Base.''; my @xFile = grep {-f $_} glob( "$s_DIR/x*.csv"); for my $csvfile (@xFile){ # input my ($name,$dir,$ext) = fileparse($csvfile, qr/\.[^.]*/ ); print "processing $name \n"; open my $IN,'<',$csvfile or die "Could not open $csvfile : $!"; # outputs my $f_Pass = $p_DIR."/pass_table_".$name.'.txt'; my $f_Fail = $f_DIR."/fail_table_".$name.'.txt'; open my $PASS,'>',$f_Pass or die "Can't open output file $f_Pass : $!"; open my $FAIL,'>',$f_Fail or die "Can't open output file $f_Fail : $!"; # process my $indexF = 0; my $indexP = 0; my $csv = Text::CSV_XS->new({ auto_diag => 1, binary => 1, callbacks => { after_parse => sub { $_ ||= 0 for @{$_[1] } }, } }); my $header = $csv->getline($IN); while ( my $colref = $csv->getline($IN) ){ my $col0 = shift @$colref; my $col1 = shift @$colref; if( $col1 == 1 ){ print $PASS join " ",@$colref,"\n"; $indexP = $indexP + 1; } else { print $FAIL join " ",@$colref,"\n"; $indexF = $indexF + 1; }; }; # report print " totalP $indexP totalF ".($indexF-0)." total ".($indexP+$inde +xF)." \n"; printf "%% totalP/(totalF+totalP) = %.2f %% \n",($indexP/($indexP+$i +ndexF)*100); close $PASS or die "Couldn't close output file $f_Pass : $!"; close $FAIL or die "Couldn't close output file $f_Fail : $!"; }; my $t1 = new Benchmark; my $td = timediff($t1, $t0); printf "\nCode took: %s\n",timestr($td); printf "++Finished program in -> %5.2f seconds\n\n",time-$start;
poj


Comment on Re^3: csv parsing with multiple missing values/multiple commas
Download Code
Re^4: csv parsing with multiple missing values/multiple commas
by f77coder (Sexton) on Aug 02, 2014 at 19:28 UTC

    Wow, this works great.

    You switched to Text::CSV_XS instead of Parse::CSV. Were there that many problems with my implementation or do you think there is a bug in Parse::CSV?

    Again many thanks

      I couldn't get the callbacks to work with Parse::CSV. I think it only works with getline not fetch.
      poj

        Can I thread/fork this code safely?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1096026]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (15)
As of 2014-10-23 15:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (125 votes), past polls