in reply to Re^4: Word Count and Match
in thread Word Count and Match

The first problem that I had with your code was the confusing name, $namecnt. I expected a numeric scalar for that type of name!
I changed that var name to "$names2count" to imply multiple names which will be counted - I assume in a case insensitive manner.
Added: William test case
use strict; use warnings; my %name_count; my $names2cnt='David|Tom|Sam|Will|Dave|William|Thomas'; while (my $line = <DATA>) { next unless ($line =~ /\S/); #skip blank lines my $name = (split (":",$line))[2]; $name_count{$1}++ if $names2cnt =~ /\b($name)\b/i; } foreach my $name (sort keys %name_count) { print "$name => $name_count{$name}\n"; } =prints: Dave => 1 Will => 3 #allows Will and WILL and WiLL spellings William => 1 =cut __DATA__ 1:NAME:Bob:Bobville:Phone 2:NAME:Dave:Davis:Phone 3:NAME:Will:Willard:Phone 4:NAME:Todd:Toadlane:Phone 5:NAME:WILL:Street:Phone 6:NAME:WiLL:Street2:phone2 7:NAME:WilliaM:xyz:1234
Update: I'm not sure that this \b stuff in the regex is necessary. I put some obvious test cases into the code, but not all possible test cases.