in reply to Re: Batch processing of files
in thread Batch processing of files
use strict; use warnings; use Fcntl qw( SEEK_SET ); my @files = <files*.txt>; # Should really use File::Temp my $fn_out = "tmp.txt"; my $error = 0; for my $fn_in (@files) { open(my $fh_out, '>', $fn_out) or die("Can't create output file \"$fn_out\": $!\n"); open(my $fh_in, '<', $fn_in) or do { warn("Can't open input file \"$fn_in\": $!\n"); $error = 1; next; }; my $sum = 0; my $cnt = 0; while (<$fh_in>) { chomp; $sum += ( split /\|/ )[2]; ++$cnt; } my $avg = $sum / $cti; seek($fh_in, 0, SEEK_SET) or do { warn("Can't seek in input file \"$fn_in\": $!\n"); $error = 1; next; }; while (<$fh_in>) { chomp; my ($hd, $md, $tl) = split(/\|/, $_); my $tlf = sprintf("%.4f", $tl/$avg); print $fh_out "$hd|$md|$tlf\n"; } close($fh_in); close($fh_out) or do { warn("Can't save to output file \"$fn_out\": $!\n"); $error = 1; next; }; rename($fn_out, $fn_in) or do { warn("Can't rename \"$fn_out\" to \"$fn_in\": $!\n"); $error = 1; next; }; } exit($error);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Batch processing of files
by austinby (Initiate) on Feb 11, 2010 at 19:14 UTC | |
by austinby (Initiate) on Feb 11, 2010 at 20:17 UTC | |
by ikegami (Patriarch) on Feb 11, 2010 at 22:35 UTC | |
by austinby (Initiate) on Feb 12, 2010 at 14:26 UTC | |
by Corion (Patriarch) on Feb 12, 2010 at 14:29 UTC |