#!/usr/bin/env perl use strict; use warnings; my %count; my $namecnt='David|Tom|Sam|Will|Dave|William|Thomas'; while(){ 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{$word}, $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 #### (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)