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?
--"The first rule of Perl club is you do not talk about
Perl club."
-- Chip Salzenberg
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Potential Ternary Operator Bug
by merlyn (Sage) on Dec 10, 2001 at 21:13 UTC | |
by robin (Chaplain) on Dec 11, 2001 at 18:49 UTC | |
Re: Potential Ternary Operator Bug
by chipmunk (Parson) on Dec 10, 2001 at 23:24 UTC | |
All code blocks are not created equal: HINT_BLOCK_SCOPE
by chip (Curate) on Dec 11, 2001 at 02:44 UTC | |
Re: Potential Ternary Operator Bug
by chromatic (Archbishop) on Dec 11, 2001 at 02:30 UTC | |
Re: Potential Ternary Operator Bug
by blakem (Monsignor) on Dec 11, 2001 at 00:38 UTC |