zilore mumba wrote:
> Once more Dear kind Perl users, I have a small problem, being so raw in 
> perl. I have binary files which contain some headers of the type nx 70 
> ny 1, and nxny 3542 ny 1.
> I want to replace all ocurrences of nx by 71, ny by 71 and nxny by 5041.
> The code below does not give any error but I end up with files of size 0 
> and the same zero size files are copied to current directory where the 
> scrip is located.
> Help will be appreciated.

I don't follow your replacement rules, but I fixed the rest and attempted
your rules literally.  I didn't attempt to change the basic logic.

use strict;
use warnings;
use POSIX;
use File::Path;
use File::Copy;

my $Grib_dir = 'grib_files';

opendir DIR, $Grib_dir or die "opendir failed on $Grib_dir: $! ($^E)";
while (my $file = readdir DIR) {

        next if -d $file;

        # Extra filtering, to copy only grib files ending in H
        next unless $file =~ /H$/;

        print "Doing $file\n" if $debug;
        my @lines = ();
        open IN, "+<$Grib_dir/$file" or die "open '$file': $! ($^E)";
        binmode IN;
        while (<IN>) {

#               s/nxny {4}{ny 1}/{nxny 5041}/g;         # ?????
#               s/nx d{4} {ny 1}/{nx 71 ny 71}/g;       # ?????

                # your stated rules (probably more to it than you stated):
                #       nxny => 5041
                #       nx => 71
                #       ny => 71

                s/nxny/5041/g;
                s/(nx|ny)/71/g;
                push @lines, $_;
        }
        seek IN, SEEK_SET, 0;           # rewind file
        print IN @lines;
        close IN;
}
closedir DIR;

__END__
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to