joshmcbride95 has asked for the wisdom of the Perl Monks concerning the following question:

hi guys, I have a script that opens a csv containing 50 columns, and splits it into two files, the first containing 30 columns, and the second containing 20 columns. I now need to re-open these files and delete rows where the column 0 is null and column 4 is null.
open my $in, '<:encoding(utf8)', $filepath_in or die; open my $courier_out_temp, '>:encoding(utf8)', $filepath_out_temp_cour +ier or die; while ( my $row = $csv->getline( $in ) ) { splice @$row, 0, 38; my $status = $csv->combine(@$row); if ($status) { print $courier_out_temp $csv->string(), "\n"; } }
I have this script, which works fine, and have tried various approaches to delete the records. Can anyone help?

Replies are listed 'Best First'.
Re: Delete lines from CSV where column cell value is null
by choroba (Cardinal) on Nov 24, 2018 at 22:59 UTC
    You can use a filter:
    #!/usr/bin/perl use strict; use warnings; use Text::CSV_XS qw{ csv }; csv(in => shift, eol => "\n", filter => {1 => sub { length }, 5 => sub { length }});

    It will only keep the lines where columns 1 and 5 (1-based) have a non-zero length.

    Tested on:

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,