Your problem is that tr/// is not a regex operator in the true sense. It always operates character by character.

What you have actually asked perl to do with the line

my $count = ($str =~ tr/(the)//);

One way to count the occurances of a given word in a string would be to use the m// operator with the /g option and force a list context as in

my $str = "The quick brown fox jumps over the lazy dog"; my $count = () = $str =~ m/(the)/ig; print $count;

will print 2.

However, that is still not quite right as using it on the string 'There are three theatres in the town' and it will print 3! This is because the regex /(the)/ will also match the first 3 chars of 'There' and 'theatre'. To ensure that you will only match whole words you can bracket the work with \b - 'word boundary zero-width assertions' like this

my $str = "There are three theatres in the town"; my $count = () = $str =~ m/(\bthe\b)/ig; print $count;

which will correctly print 1.

my $str = "The quick brown fox jumps over the lazy dog"; my $count = () = $str =~ m/(\bthe\b)/ig; print $count;

which will correctly print 2. (Note: the /i modifier to make the match case independant.)


Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.


In reply to Re: counting the number of occurrances of a word using regex by BrowserUk
in thread counting the number of occurrances of a word using regex by abhishes

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.