Back in the good ol' days, push and its ilk could take as their first argument an EXPR that evaluated to an unblessed array reference, making the syntax slightly less convoluted:
c:\@Work\Perl>perl -wMstrict -MData::Dump -le
"print qq{perl version $]};
;;
my (@foo, @bar);
my $fooish = 1;
push $fooish ? \@foo : \@bar, 'quux';
dd \@foo;
dd \@bar;
"
perl version 5.014004
["quux"]
[]
Today, we have Postfix Dereference Syntax (see perlref). Ah, the good ol' days... (sigh)
Update: To avoid leading anyone astray, I should have mentioned that, per the docs: "Starting with Perl 5.14, an experimental feature allowed push to take a scalar expression. This experiment has been deemed unsuccessful, and was removed as of Perl 5.24." (This behavior was also removed from related built-ins pop, keys, values, and other such functions that, for a time, operated by reference on arrays and hashes.) (Update: For the official announcement, see The autoderef feature has been removed in perl5240delta.)
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
for my $num (@$aref) {
$flag = 1 if $num == 6;
push $flag ? \@temp : \@result, $num;
do {@temp = (); $flag = 0;} if $num == 7;
}
}
push @result, @temp;
print Dumper \@result;
This is working fine on the few data samples that I used.
I just did not think about using array refs instead of arrays to get the ternary operator to work in this context. Thank you, AnomalousMonk, for showing it, I've just learned something.
That said, the fact that I did not find the right syntax to do it with the ternary operator prompted me to use an array ref in another fashion (instead of a $flag) which is quite concise and, IMHO, relatively elegant.
| [reply] [d/l] [select] |
... I've just learned something.
Unfortunately, you've just learned something that is obsolete. I should have made it clear that this feature, added with Perl version 5.14, was removed with version 5.24. I've added an update to this effect to the previous node. Sorry for any inconvenience.
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] |