Hello Melly,
I see you’ve been given detailed explanations of the first snippet, which uses the transliteration operator tr///, but not of the second, which uses the substitution operator s///:
my $count = length( $str =~ s/[^\Q$char\E]//rg );
There is actually quite a bit of detail to unpack here. The effect of the code is to create a string from which all non-$char characters have been removed, and then count the remaining characters in that string (i.e., just the $char characters). Removal of the non-$char characters is accomplished by replacing them with the empty string in the replacement part of the substitution.
Hope that helps,
| Athanasius <°(((>< contra mundum | סתם עוד האקר של פרל, |
In reply to Re: Confused by RegEx count
by Athanasius
in thread Confused by RegEx count
by Melly
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |