davorg has asked for the wisdom of the Perl Monks concerning the following question:
This just came up in the CB. Someone wanted to filter an array into two arrays. I decided to be clever and suggested this:
push @{ ($_ % 2) ? @odd : @even }, $_ foreach @numbers;
That works as expected, but it turned out that the filtering should be done on a regex match. I changed my code to:
push @{ (/PAT/) ? @match : @nonmatch }, $_ foreach @words;
But that gives a "Bizarre copy of ARRAY in leave" error. Changing it again to:
push @{ (/PAT/) ? \@match : \@nonmatch }, $_ foreach @words;
gets it working again. And on reflection, that's what should work. So, I guess the original version shouldn't work (you should need to take references in that case as well). But it does. Any explaination?
--<http://www.dave.org.uk>
"The first rule of Perl club is you do not talk about
Perl club."
-- Chip Salzenberg
Back to
Seekers of Perl Wisdom