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);

In reply to Re: Counting strings not counting correctly. by prysmatik
in thread Counting strings not counting correctly. by new2perl2016

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.