Your sample code is not a short self-contained snippet that demonstrates the behavior you describe. Your data is not part of the code, and the code that is relevant is incorporated in a subroutine that your example never calls, and it has external dependencies that aren't needed, and that aren't loaded by the snippet. That means anyone wanting to respond to help you has to refactor the code so that it can run stand-alone, and in doing that, anyone trying to help you might inadvertently fix the thing that you're describing as working incorrectly. Precision in these sorts of things is important. We shouldn't be chasing you down to demonstrate the actual bug to us in a way that we can repeat in our own tests.
I took a stab at doing this; I pulled the data into a __DATA__ segment, removed the need for external files, and caused your output to print entirely to STDOUT, but preserved in the output the names of the handles you were printing to. I also removed the colorization, since it is an external dependency that you didn't "use" in your snippet. So I assume it's not part of the problem. Additionally, I added the formatting back in that I needed to be able to understand your code. Having done all that stuff that you should have done, this is what I came up with:
#!/usr/bin/env perl
use strict;
use warnings;
my %count;
my $namecnt='David|Tom|Sam|Will|Dave|William|Thomas';
while(<DATA>){
my @words = split(":");
foreach my $word (@words){
if($word=~/($namecnt)/io){
$count{$1}++;
}
}
}
foreach my $word (sort keys %count) {
printf("(STDOUT) %39s %-14s %-19s %6s", "There are", $count{$wo
+rd}, $word, "Name(s)\n");
print "(OUTPUT) There are $count{$word} $word Name(s)\n";
}
__DATA__
1:NAME:Bob:Phone
2:NAME:Dave:Phone
3:NAME:Will:Phone
4:NAME:Todd:Phone
And when I run that I get:
(STDOUT) There are 1 Dave
+ Name(s)
(OUTPUT) There are 1 Dave Name(s)
(STDOUT) There are 1 Will
+ Name(s)
(OUTPUT) There are 1 Will Name(s)
Which, to me, is an indication that the code is behaving as designed, and that you are NOT getting some summary count at the end like you said you are getting. At least not from the code you provided.
So where are we now? You've asked a question, people said that the original question didn't demonstrate the problem being described. You took a stab at providing a better example of the code, but still failed to demonstrate that there is actually a problem with the code you posted. If I had to guess, I would say you have a print statement somewhere that you have forgotten about. Either way, people who are just trying to help had their willingness to help squandered.
|