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.
(STDOUT) There are 1 Dave Name(s)#!/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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Word Count and Match
by choroba (Cardinal) on Jan 08, 2021 at 13:44 UTC | |
|
Re^5: Word Count and Match
by LanX (Saint) on Jan 08, 2021 at 13:41 UTC | |
|
Re^5: Word Count and Match
by davido (Cardinal) on Jan 08, 2021 at 19:58 UTC | |
|
Re^5: Word Count and Match
by eyepopslikeamosquito (Archbishop) on Jan 08, 2021 at 15:51 UTC | |
|
Re^5: Word Count and Match
by Marshall (Canon) on Jan 09, 2021 at 20:16 UTC |