e-letter wrote:
> Readers,
> 
> I have a csv file (greater than 256 columns hence unable to open in
> spreadsheet) of the following format:
> 
> column header1, column header2, column header3
> 1,0.0e0,0.0e0,5e-6
> 2,0.0e0,0.0e0,6e-7
> 3,0.0e0,0.0e0,0.0e0
> 
> I want to perform: "if column headerx contains only values of 0.0e0,
> delete the column (including the column header).
> 
> How do I start please?

Here's a solution using Text::CSV and a lot of help from the very lovely
List::MoreUtils.

HTH,

Rob


use strict;
use warnings;

use IO::Handle;
use Fcntl qw/SEEK_SET/;
use Text::CSV;
use List::MoreUtils qw/indexes/;

open my $fh, '<', 'input.csv' or die $!;

my $csv = Text::CSV->new;

my @keep = do {
  my $header = $csv->getline($fh) or die $csv->error_diag;
  (0) x @$header;
};

while (my $row = $csv->getline($fh)) {
  $keep[$_]++ foreach indexes { $_ ne '0.0e0' } @$row;
}

seek $fh, 0, SEEK_SET;

@keep = indexes { $_ } @keep;

while (my $row = $csv->getline($fh)) {
  $csv->combine(@[EMAIL PROTECTED]);
  print $csv->string, "\n";
}

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to