in reply to Re^4: Ignore a range of numbers ina List
in thread Ignore a range of numbers ina List

It seems you need
push @{ $flag ? \@temp : \@result }, $num;
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^6: Ignore a range of numbers ina List
by Laurent_R (Canon) on Jun 26, 2017 at 06:11 UTC
    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.

      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:  <%-{-{-{-<

        Hi AnomalousMonk,

        I'm actually running these tests on 5.14.4, and the syntax you've shown is exactly what I was after to shorten the loop:

        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.