Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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

by Tanktalus (Canon)
on Mar 24, 2005 at 18:45 UTC ( #442157=note: print w/replies, xml ) Need Help??

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

The grep is superfluous and, IMO, only distracts from the code. Here's the sub to add to the above benchmark:

TANKTALUS => sub { my %copy = %hash; @copy{keys %add} = values %add; },
And the benchmarks. First, the original 500/500 overlap/new keys:
Rate ZAXO NULL TANKTALUS JAPHY BASE ZAXO 12.3/s -- -23% -59% -59% -76% NULL 16.1/s 30% -- -46% -47% -68% TANKTALUS 29.8/s 142% 85% -- -2% -41% JAPHY 30.4/s 147% 89% 2% -- -40% BASE 50.9/s 313% 216% 71% 67% --
As you can see, the difference is pretty insignificant compared to the extra "line noise" that needs to be parsed by the human who maintains it. I didn't think it'd change much with a larger sample, but I tried it anyway (using your 5_001..12_000 hash):
Rate ZAXO NULL JAPHY TANKTALUS BASE ZAXO 9.48/s -- -39% -55% -55% -81% NULL 15.7/s 65% -- -26% -26% -69% JAPHY 21.1/s 122% 35% -- -0% -59% TANKTALUS 21.1/s 123% 35% 0% -- -59% BASE 51.1/s 439% 226% 142% 142% --
Somewhere, deep down in the lost precision, the non-grep version comes out ahead. Which we'll attribute to statistical insigificance, just as I did for the 2% in the other direction. ;-)

The other point is that your answer does not do the same thing as the rest of the examples here. In all the rest of the cases, the new hash always overrides the values in the old hash. That is, if the old hash had a => "b", while the new hash had a => "c", the behaviour of yours vs the rest is different. Which one is correct is dependant on the demands of the situation, although I usually lean towards new hashes winning over old hashes the way I write the code. In cases where I want "first come, first served" policies, then I will definitely keep your grep in mind - it's probably a lot more elegant than the way I have been doing it (if I have - I'd have to look through old code to be sure). Thanks ;-)

Replies are listed 'Best First'.
Re^7: Concerning hash operations (appending, concatenating)
by japhy (Canon) on Mar 24, 2005 at 18:50 UTC
    You missed the point, Tanktalus. We're only adding NEW key-values pairs to the hash, thus the grep() is required. The node by Zaxo was showing how to have the original values override the new values if the keys matched.
    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://442157]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (1)
As of 2018-07-23 04:20 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (459 votes). Check out past polls.