First, I congratulate you! This idea of using match global in a list context is a great one!

I wasn't exactly sure what you wanted as there is some ambiguity in the spec. I interpreted the intent to be: number of the strings that matched anywhere (not total number of matches of those strings) and which ones were they? Code below can be tweaked to do different things.

A few points that you may find interesting is that
1.To read the entire file into a single Perl variable, no while{} loop is required, just undef the record separator and do a scalar assignment! BTW, you will often see @var=(),$var=() and this has same effect as =undef.
2. It is possible to increment a hash value that doesn't even exist yet! Perl will create it and set value =1 on the first increment.
3. You will see that I "cheated" in the print. The concatenation op like: "".keys(%seen) forces keys into a scalar context, scalar keys(%seen) would have been fine too, but this is just a fine point that you may find useful later, eg, print "blah".@array is different than print "blah", @array.
4. I took the /m out of the regex, you may need this if a string spans across \n boundaries.
5. You can expand this code to say print a table of number of times each string matched, etc.

#!/usr/bin/perl -w use strict; my %seen; $/=undef; #input record separator doesn't matter any more my $data = <DATA>; #whole file is read into one variable! my @match = ( $data =~ /(first_string|second_string|third_string|fourt +h_string|fifth_string)/g ); foreach (@match) { $seen{$_}++; #yes, you can increment a hash key that #doesn't exit yet! } #now we have a hash of strings that were seen. #also know the number times each string was seen. #but that doesn't appear to be necessary to know that here? print "".keys(%seen)," matches found: ", join(", ",sort keys(%seen))," +\n"; __DATA__ This is a test file matchme ljldjlfjd l;djfldjlf d test test test dljfldjlfjldjfldjlljdf one second_string dlfjldfj ljdfldjjf ldjfljdl dfljdlfj dfdlfj three ljfldjlj dlfjlasdj foiidufoiida matchdf dljfldsaofuoidfousdaof ladsjflasdof first_string dlfjodsuofuasdo sadoufosadu foasduf aosduf third_string __END__ __above prints: 3 matches found: first_string, second_string, third_string

In reply to Re: Printing the count for regex matches by Marshall
in thread Printing the count for regex matches by learningperl01

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.