So... an adaptation of the code you presented works as you seem to want:

#!/usr/bin/env perl use strict; use warnings; my %count; my @words = qw(Bob Tom Dave John Jeremy Max Tom Harold Tom Bob Pete Pe +te Frank Tom); my $wordcnt='Bob|Tom|Dave|Tom|Bob|Dave'; foreach my $word (@words){ if($word=~/($wordcnt)/io){ $count{$1}++; } } print "$_ => $count{$_}\n" for sort keys %count;

This produces:

Bob => 2 Dave => 1 Tom => 4

There's no extra output with some total count.

Your problem would be a lot easier to diagnose if you supplied us with the following:

Currently we're debugging code that probably isn't the code you are running, and we're only able to guess at what the data looks like.

I don't think you need the /o modifier for your regular expression; it doesn't do anything useful nowadays. And it's probably better to use $wordcnt = qr/Bob|Tom..../; instead regular quotes, though that's not strictly necessary. You should probably surround your pattern match with \b anchors for word boundaries so that you don't match "Bob" for the name "Bobby". Your alternation is also silly; what are repeated alternates supposed to do?

I scanned back through some of your previous posts over the past seventeen years; you've been asked to provide real code and sample input and output in the past.


Dave


In reply to Re: Word Count and Match by davido
in thread Word Count and Match by PilotinControl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.