in reply to Counting strings not counting correctly.
I'm too dusty to offer solid response, but the problem is in your looping structure.
Your while should close before that last foreach. And if you're creative you can reduce those 2 foreach's in the middle to 1.
This may do more harm than good, and probably won't run, but it should point in the right direction.
#!/usr/bin/perl use strict; use warnings; use MIME::Lite; use File::stat; use Time::localtime; use List::Util qw(sum); my $OUT; my %Accounts = ('AE202B01'=>0, 'AE202B02'=>0, 'AE202B03'=>0); my $emd = `date --date '-30 min' +'%H'`; chop ($emd); $emd = "/tmp/pathtomyfiles/$emd.txt"; my $outfile = "/tmp/scripts/AESTUFF/AE202b.txt"; open (my $fh1, "-|" , "$emd") || die "Failed: $!\n" ; open ($OUT, "> $outfile") || die "Cannot open $outfile"; # temp file t +o which to write the formated output, if there is a match while (my $line = <$fh1>) { chomp ($line); $line =~ s/ /\n/; if ( $line =~ /UserId1:(\w+),pairId.*/ ) { foreach $accounted_account ( keys @Accounts ){ if ($accounted_account==$1){ $Accounts{$accounted_account}++; } } } } foreach my $counted (sort keys %Accounts) { printf OUT "%-31s %s\n", $counted, $Accounts{$counted}; } close ($OUT);
|
|---|