Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Concerning hash operations (appending, concatenating)

by tilly (Archbishop)
on Mar 26, 2005 at 02:10 UTC ( #442441=note: print w/ replies, xml ) Need Help??


in reply to Re: Concerning hash operations (appending, concatenating)
in thread Concerning hash operations (appending, concatenating)

General advice. Just because there are many ways to do it in Perl is not a reason for picking a less readable one. If you are not returning data from map, then you should write it like this:

$hash1{$_} = $hash2{$_} for keys %hash2;
That is easier to read, and much more clearly signals intent. It is also at least as fast as the map version. (It used to be a lot faster, but in Perl 5.8 there is an optimization that causes map to shortcircuit to become a for if it is in null context.)

Furthermore performance is far less likely to matter than most people think, and when it does having micro-optimized as you went is generally a bad strategy for getting it. (You want to keep code clean and then look for a better algorithm, or move a small section into C.) Therefore I would generally use the following strategy because it is even clearer, even though it is marginally slower on my machine (about 10% so):

@hash1{keys %hash2} = values %hash2;
And, of course, in the rare case that performance really mattered and I really wanted to work in Perl, it is fastest to avoid having to do 2 sets of hash lookups on %hash2:
$combined{$k} = $v while my ($k, $v) = each %hash2;


Comment on Re^2: Concerning hash operations (appending, concatenating)
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (16)
As of 2014-08-29 15:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (281 votes), past polls