John W. Krahn wrote:
e-letter wrote:
Readers,

Hello,

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?

This may work for you:

#!/usr/bin/perl
use warnings;
use strict;
use Fcntl ':seek';

@ARGV = 'somefile.csv';
$^I   = '.back';

my @headers = map 1, split /,/, <>, -1;
my @keep = ( undef ) x @headers;

while ( <> ) {
    chomp;
    my @fields = split /,/, $_, -1;
    for my $i ( 0 .. $#fields ) {
        $keep[ $i ] = $i if $fields[ $i ] ne '0.0e0';
        }
    last if @headers == grep defined, @keep;

That would probably be more efficient as:

      exit 0 if @headers == grep defined, @keep;


    }

@keep = grep defined, @keep;

seek ARGV, 0, SEEK_SET or die "Cannot seek on '$ARGV' $!";

while ( <> ) {
    chomp;
    print join( ',', ( split /,/, $_, -1 )[ @keep ] ), "\n";
    }

__END__


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

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


Reply via email to