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

Re^2: I want to operate on some values in a hash

by misterperl (Pilgrim)
on Jan 04, 2018 at 16:26 UTC ( [id://1206697]=note: print w/replies, xml ) Need Help??


in reply to Re: I want to operate on some values in a hash
in thread I want to operate on some values in a hash

Eily: " modifying a set of values with map (rather than create a new set) is considered bad practice" I'd actually never read that, can you elaborate? I often do that with great success. But if there are side-effects or dangers I'm not aware of I'd like to know that.. Thanks
  • Comment on Re^2: I want to operate on some values in a hash

Replies are listed 'Best First'.
Re^3: I want to operate on some values in a hash
by Eily (Monsignor) on Jan 04, 2018 at 16:56 UTC

    I suppose you wrote that before I edited my message?

    Honestly I can't remember seeing the map in void context for a while, except when I mention that people keep bringing up the subject myself (but I did see it often in my earlier readings on perlmonks), make of that what you will.

    One reason for the discussion is that map is supposed to build a new list, and not using that output is a waste of resources. But even if map wasn't smart about its context, it's perl, you're supposed to ignore performance issues most of the time and focus on getting things done. The other issue is when you use the aliasing mechanism, ie the fact that modifying $_ will modify the input. Since ; map { $_ += 2 } @numbers; and ; $_ += 2 for @numbers; do exactly the same thing, it's clearer if you use the version that's not supposed to create a separate output. Best reason to use the second version is that you won't get told that there's a better way to do it when you post it on perlmonks :D

    NB: even without using the aliasing mechanism: ; map { $h{$_}++} @keys; may be better written as ; $h{$_}++ for @keys;, because map does something from the input, while for does something with the input. Nitpicking at its finest :)

Re^3: I want to operate on some values in a hash
by karlgoethebier (Abbot) on Jan 04, 2018 at 17:24 UTC

    It's strictly verboten since the birth of the cool AKA hour zero or so. May be one of our founders imposed this but no one remembers who it really was. But remember: Things that are verboten - like perl -E 'map {++$_; say;} 0..9' - give the real kick ;-) For a more serious debate please see map vs for\foreach..

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1206697]
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-19 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found