Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^7: Ignore a range of numbers ina List

by AnomalousMonk (Archbishop)
on Jun 26, 2017 at 13:17 UTC ( [id://1193561]=note: print w/replies, xml ) Need Help??


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

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

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

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

        Well, you know, at $work, I'm stuck with version 5.10 on our aging AIX servers (but we are moving soon to a RHEL Linux) and even with version 5.8 on our VMS servers. That's one of the reasons I am not too keen to upgrade above 5.14 at home (on Cygwin), I wouldn't be able to use new functionalities when back at work anyway. In a way, I don't have trouble with obsolete features, it is more with modern features that I have issues. I can't even use "say" on my VMS server (unless I implement it myself, which I did in at least a couple of occasions).

        To tell the full truth, I also now have a quite nice bash under Windows at home (which got installed, I believe, together with Git or Github, not sure which one), including a fairly modern Perl 5.24 (it was even the most modern version at the time). So I can try many of the modern features such as Postfix Dereference Syntax, although it has, IMHO, an ugly syntax which I'll probably never be able to memorize, so I'll have to look up the documentation anytime I would want to use it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1193561]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-23 18:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found