Let's examine the first one to explain what it is doing and how you could find this out. As usual, start at the right of the expression with tr/y// where tr is the transliteration operator. Note that this operator does not work with regular expressions but just deals with lists of characters. Every character in the first list (y) found in the operand ($str) is replaced by the equivalent character in the second list (empty) or if, as in this case, there is no equivalent character in the second list, the matching character is left as it is. tr then returns the number of characters in the operand so treated. If there were no explicit operand it would use $_ by default.

$ perl -E '$str="Fly guys try yoyos"; my $c = $str =~ tr/y//; say $c;' 5

How do you discover all this? Well, perldoc will tell you all about tr - you just need to know that the docs are the place to look. Try perldoc -f tr for all the good stuff.

With that in mind, can you now work out what is going on in the second example?


🦛


In reply to Re: Confused by RegEx count by hippo
in thread Confused by RegEx count by Melly

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.