e-letter wrote:
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__

I tried the original code above and received the following error:

Sorry that I did not test the code enough.


seek() on closed filehandle ARGV at filename.pl line 24, <> line 52.
Cannot seek osn 'filename2.csv' Bad file descriptor at filename.pl
line 24, <> line 52

Since the file has 32 lines I was confused by the reference to 'line
52', so I deleted the empty line below _END_ and then repeated the
command:

In the error message the line number following the program name (24) refers to the line in the program where the error occured and the line number following the filehandle (52) refers to the line in the data file where the error occured.

In a Perl program the __END__ token signifies to the compiler where the end of the program is and so anything after it is ignored and therefore has no effect on any error reporting.

At this point you would have had the original file truncated to zero bytes and the original data stored in the back-up file with the extention '.back'.


perl filename.pl

Nothing happened except that the cursor in the command terminal moved
to the next line and remained, flashing without the [...] prefix. I
pressed crtl c and looked at the file using mc. The result was that my
csv file was totally emptied of content, i.e. became an empty file.

At this point you could have restored the original file by moving the back-up file to the original:

mv somefile.csv.back somefile.csv


After more testing I think that this will work better:

#!/usr/bin/perl
use warnings;
use strict;


my $file = 'testfile.csv';

open my $FH, '<', $file or die "Cannot open '$file' $!";

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

while ( <$FH> ) {
    chomp;
    my @fields = split /,/, $_, -1;
die "Error: expected " . @headers . " fields but reading " . @fields . " fields instead.\n"
        if @headers < @fields;
    for my $i ( 0 .. $#fields ) {
        $keep[ $i ] = $i if $fields[ $i ] ne '0.0e0';
        }
    exit 0 if @headers == grep defined, @keep;
    }

close $FH;

@keep = grep defined, @keep;

( $^I, @ARGV ) = ( '.back2', $file );

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