Jul 5, 2000, 10:08 PM
Post #2 of 7
I agree with SixKiller (is it fair if I play, too? )
@new = map /o/ ? $& : (), qw!one two three four five!;
It's basically saying that if o matches, as it does in (one, two, and four), toss only the precise text that matched (stored in $&), which is o, into @new. If it doesn't match, it's nothing ()
However, if Cure had used:
@new = map /o/ ? $_ : (), qw!one two three four five!;
then it would have yielded the results that mckhendry posted.
Performance note: Because Perl needs to know if the exact matched data will be needed, it looks for $& at compile time. If it finds $& anywhere in the program (and even in libraries and modules that the program uses), it will take the time to store the matched data for each regex in the program, even in regexes that do not use $&. This will slow down all regexes, so it's a good rule of thumb not to use $& unless you really, really have to, especially in a library or module.
This is fun