in reply to Counting matches.
/g in scalar context (as imposed by >=) only returns the next match. This allows people to do something like
while (/a/g) { ... }
Since the result of a list assignment in scalar context is the number of elements in the assigned list, the trick is to assign the result of the match to a list, even an empty one, and place that assignment in scalar context.
my $count = () = 'baa baa black sheep' =~ /a/g;
Or as it applies here,
print( ( () = 'baa baa black sheep' =~ /a/g ) >= 3 ? 'more' : 'less' ) +;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Counting matches. (scalar list assign)
by ikegami (Patriarch) on Dec 07, 2007 at 08:25 UTC |