Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
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 having an uproarious good time at the Monastery: (20)
As of 2015-07-29 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls