Hello Monks Can you explain to me why my code below does not work? I have a file handle open to extract a few account id's and I need to count them. Desired output

AE202B01 5

AE202B02 3

AE202B03 2

but the output comes out:

AE202B01 1

AE202B01 1

AE202B01 1

AE202B01 1

AE202B01 1

AE202B02 1

AE202B02 1

AE202B02 1

AE202B03 1

AE202B03 1

can you please tell me what is wrong

#!/usr/bin/perl use strict; use warnings; use MIME::Lite; use File::stat; use Time::localtime; use List::Util qw(sum); my $Accounts; my $count; my $counts; my $OUT; my $char; my @Accounts = ('AE202B01','AE202B02','AE202B03'); my $emd = `date --date '-30 min' +'%H'`; chop ($emd); $emd = "/tmp/pathtomyfiles/$emd.txt"; my %count; my $outfile = "/tmp/scripts/AESTUFF/AE202b.txt"; open (my $fh1, "-|" , "$emd") || die "Failed: $!\n" ; open ($OUT, "> $ +outfile") || die "Cannot open $outfile"; # temp file to which to writ +e the formated output, if there is a match while (my $line = <$fh1>) { chomp ($line); $line =~ s/ /\n/; # $line =~ s/.*UserId1://; # $line =~ s/}.*//; # $line =~ s/,pairId:.*//; if ( $line =~ /UserId1:(\w+),pairId.*/ ) { $Accounts = $1; foreach (@Accounts) { if ($Accounts =~ /$_/ ){ foreach my $str (split /\s+/, $Accounts) { $count{$str}++; } } foreach my $str (sort keys %count) { printf "%-31s %s\n", $str, $count{$str}; } } } } #} close ($OUT);

Your feedback is greatly appreciated


In reply to 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.