Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Hashes for counting

by fullermd (Curate)
on Aug 07, 2010 at 10:32 UTC ( #853535=note: print w/ replies, xml ) Need Help??


in reply to Hashes for counting

Well, for one thing, this line:

$has{$peer_address}->{times_connect}=$has{$peer_address}->{times_conne +ct}+1;

is way more complicated than necessary. Firstly, you don't need the internal -> dereferences, and second, why repeat yourself instead of just using an increment?

$has{$peer_address}{times_connect}++;

Second, since (at least in the code you're giving) you're not tracking anything but the times_connect, you don't need that extra layer anyway; just make $has{$peer_address} the count:

$has{$peer_address}++;

(of course, if you're tracking more info in reality you wouldn't want to do that, but...)

And third, since perl data structure autovivify, you don't need the branch doing special stuff based on whether $has{$peer_address} already exists. You can just do the increment unconditionally, and it'll start at 0++ (a.k.a., 1) if it's not already set.

Since you have ++$has{$peer_address}; in your else branch, it rather reads like you sorta intended to do this in the first place, but then you do other weird stuff elsewhere...


Comment on Re: Hashes for counting
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-12-28 18:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls