Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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)

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2016-10-23 12:32 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (301 votes). Check out past polls.