John W. Krahn wrote:
John W. Krahn wrote:
I rewrote your code so that:
1) It opens the log file once at the start;
2) Adds more error checking;
3) Prints an accurate count of input and output lines;
4) Preserves the same order of the input lines on output;
so hopefully this will work better for you.
#!/usr/bin/perl
use warnings;
use strict;
my $parent_path =
'/home/user71/RangerDatasource/Customization/TelekomMalaysia/Scripts/Tests/cprogs/files';
my $child_path = "$parent_path/child";
my $write_path = "$parent_path/output";
my $continue = 1;
$SIG{ INT } = $SIG{ TERM } = sub { $continue = 0 };
# open log file and autoflush it
open my $LOG_FILE, '>>', "$write_path/logfile2" or die "Cannot open
'$write_path/logfile2' $!";
select( ( select( $LOG_FILE ), $| = 1 )[ 0 ] );
my %times;
while ( $continue ) {
opendir my $dh, $child_path or die "Cannot open '$child_path' $!";
for my $file ( grep -f "$child_path/$file", readdir $dh ) {
** Correction **
for my $file ( grep -f "$child_path/$_", readdir $dh ) {
unless ( exists $times{ $file } ) {
open my $IN_FILE, '<', "$parent_path/$file" or die "Cannot
open '$parent_path/$file' $!";
my $count = 0;
my %hash;
while ( my $line = <$IN_FILE> ) {
$count++;
my $key = join ',', ( split /,/ )[ 2, 3, 6, 7 ];
Opps, another correction:
my $key = join ',', ( split /,/, $line )[ 2, 3, 6, 7 ];
$hash{ $key } = [ $., $line ];
}
close $IN_FILE;
print $LOG_FILE "Count of cdrs before removing duplicates
= $count\n";
open my $OUT_FILE, '>', "$write_path/$file.out" or die
"Cannot open '$write_path/$file.out' $!";
$count = 0;
for my $line ( map $hash{ $_ }[ 1 ], sort { $hash{ $a }[ 0
] <=> $hash{ $b }[ 0 ] } keys %hash ) {
$count++;
print $OUT_FILE $line;
}
close $OUT_FILE;
print $LOG_FILE "Count of cdrs after removing duplicates =
$count\n";
}
unlink "$parent_path/$file" or warn "Cannot unlink
'$parent_path/$file' $!";
unlink "$child_path/$file" or warn "Cannot unlink
'$child_path/$file' $!";
}
closedir $dh;
sleep 2;
}
close $LOG_FILE;
__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/