Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^4: map vs for\foreach.

by ikegami (Patriarch)
on Mar 13, 2015 at 17:39 UTC ( [id://1119980]=note: print w/replies, xml ) Need Help??


in reply to Re^3: map vs for\foreach.
in thread map vs for\foreach.

Superstition makes me post-increment; I think it’s not slower anymore but was back in the dinosaur age.

Post-increment is necessarily slower since it requires making a copy of the scalar that pre-increment doesn't. However, Perl replaces post-increment operators in void context with pre-increment operators.

$ perl -MO=Deparse -e'$_++ for @a;' ++$_ foreach (@a); -e syntax OK $ perl -MO=Deparse -e'map { $_++ } @a;' map {$_++;} @a; -e syntax OK

Replies are listed 'Best First'.
Re^5: map vs for\foreach.
by karlgoethebier (Abbot) on Mar 13, 2015 at 18:31 UTC

    BTW:

    #!/usr/bin/env perl use strict; use warnings; use Data::Dump; my @original_data_set = ( 1 .. 10 ); dd \@original_data_set; my @modified_data_set = map { $_++} @original_data_set; dd \@original_data_set, \@modified_data_set; __END__ karl-mac-mini:monks karl$ ./1119980.pl [1 .. 10] ([2 .. 11], [1 .. 10])

    Best regards, karl

    «The Crux of the Biscuit is the Apostrophe»

      So the so called "modified" set isn't really modified because its a post increment.

      While being consistent and logic it's indeed surprising, and a good reason to generally prefer pre-increments.

      edit

      Thanks seems like I have to rethink my habit.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)

      PS: Je suis Charlie!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-26 03:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found