Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: [OT]: This does not look like start end (grep vice for)

by AnomalousMonk (Archbishop)
on Apr 10, 2018 at 20:02 UTC ( [id://1212651]=note: print w/replies, xml ) Need Help??


in reply to This does not look like start end

WRT this reply:

grep { croak "This doesn't look like start-end\n" unless /(.*)-(.*)/; push @a, $1; push @b, $2; } @cidr;
I've seen map (mis)used in place of a for-loop before (although I've never understood the attraction of this unclean practice), but never grep. Purely to satisfy my idle curiosity, can anyone speculate on the rationale for this odd syntactic tic? Why not just
for (@cidr) { croak "This doesn't look like start-end\n" unless /(.*)-(.*)/; push @a, $1; push @b, $2; }


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: [OT]: This does not look like start end (grep vice for)
by pryrt (Abbot) on Apr 11, 2018 at 15:19 UTC

    My thought was that somewhere in the development line, the author had switched from grepping into a single array to push @a...; push @b...;. But I went back all the way to the v0.02 (the farthest that metacpan would take me in BackPAN), and it was still the grep { ... push ... }-in-void-context construct. So if it was the change to push that caused it, the change happened before it was tracked in CPAN.

      ... switched from grepping into a single array to push @a...; push @b...;.

      But there are many examples of statements like  grep { $_=0 } @acopy; Very strange.


      Give a man a fish:  <%-{-{-{-<

Re^2: [OT]: This does not look like start end (grep vice for)
by karlgoethebier (Abbot) on Apr 11, 2018 at 14:07 UTC
    "...speculate on the rationale for this odd syntactic tic?"

    No problem at all:

    • Someone told the author that grep is cool
    • Someone told the author that grep is for real men
    • Someone told the author that grep is cooler than foreach
    • The author doesn't know foreach
    • Other reason

    Hey - it looks a bit like a poll.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      ◦ The author doesn't know foreach

      In support of which we have, just three lines past the grep statement:

      my $i; for ($i=0; $i <= $#a; $i++) { next if _ipcmp($b[$i], $lo) < 0; next if _ipcmp($hi, $a[$i]) < 0; return 1; }
      A genuine Perlish for-loop written in C-style. This peculiarity and another grep-loop are found also in cidradd() at line 985. I haven't looked further at the code. OTOH, in the very next function defined after cidrlookup(), cidrvalidate(), we see four kosher Perlish for-loops and a properly (IMHO) used grep, although I must admit there is no guarantee both functions were written by the same programmer. This is doing my nut.


      Give a man a fish:  <%-{-{-{-<

        "...doing my nut."

        You may invite the author to some code audit. Programmers like mail from the spanish inquisition ;-)

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-20 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found