On Thu, Jun 14, 2001 at 05:08:30PM -0400, Carl Rogers wrote:
>
> >
> >$string =~ /a/g; # match on all 'a' in $string
> >
> >Now how do I know how many times it actually matched?
>
> Try:
> $result = $string =~ /a/g;
> The value will be in $result
> perl -wle '$str = "abcabcabc"; $cnt = $str =~ /a/g; print $cnt;'
1
What you really meant was:
> perl -wle '$str = "abcabcabc"; $cnt = () = $str =~ /a/g; print $cnt;'
3
Which forces the /a/g to be evaluated in list context, the return value of
which is then evaluated in scalar context. The weird thing is it's
evaluated not as most lists in scalar context, which evaluate to the last
element, but as if it were an array, resulting in the count of elements.
But, if you're counting a single character:
> perl -wle '$str = "abcabcabc"; $cnt = $str =~ tr/a/a/; print $cnt;'
3
Michael
--
Administrator www.shoebox.net
Programmer, System Administrator www.gallanttech.com
--