Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: Looping Over Hash Skips an Element?

by Eliya (Vicar)
on Jan 06, 2012 at 18:42 UTC ( #946647=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Looping Over Hash Skips an Element?
in thread Looping Over Hash Skips an Element?

if ($counter <= 8) { $sum_values[$x] += $ifSpeeds{ $key }; $counter++; } else { $x++; $sum_values[$x] = $ifSpeeds{ $key }; $counter = 1; }

To avoid the almost-duplication of the key statement, you could also write

$sum_values[$x] += $ifSpeeds{ $key }; $x++ unless ++$counter % 8;

or even simply

$sum_values[$counter++/8] += $ifSpeeds{ $key }; # the [] impli +es int(...)

(Perl does not generate a warning when you do something like $sum += ..., with $sum being undef initially — it treats it as 0)


Comment on Re^3: Looping Over Hash Skips an Element?
Select or Download Code

Log In?
Username:
Password:

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

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

    The best computer themed movie is:











    Results (238 votes), past polls