Hi choroba,
thanks for your input. This certainly looks quite clever. I can't test right now on my mobile device, but certainly will as soon as possible.
Update: I tried it now, and it works just fine.
| [reply] |
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] |