http://qs321.pair.com?node_id=11126600


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

@Dave: There actually is a problem with the code. I've modified the code below and the output. As you can see it lists the names twice instead of just once. That's why I wanted to match the data exactly using the specific field in the file.

#!/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:Bobville:Phone 2:NAME:Dave:Davis:Phone 3:NAME:Will:Willard:Phone 4:NAME:Todd:Toadlane:Phone
(STDOUT) There are 1 Dave Name(s)
(OUTPUT) There are 1 Dave Name(s)
(STDOUT) There are 2 Will Name(s)
(OUTPUT) There are 2 Will Name(s)