Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Mathematical averages

by arrow (Friar)
on Nov 22, 2002 at 22:14 UTC ( [id://215265]=perlquestion: print w/replies, xml ) Need Help??

arrow has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Perl Monks! I'm working on a rather simple script, but one small part keeps giving me trouble. It is supposed to give an average, but skip nonexistant entries. Maybe I can explain better with the actual code:
for (my $num = $members) { foreach $month2 (@calendar) { foreach $name2 (@members) { if ($hoursWorked{$name2}{$month2} == 0) { $num--; #this is to protect against false averages, because $members w +ill usually have a value of 13, but I don't want to count the zero va +lues because is would result in an incorrect average, (i.e. 24/6 inst +ead of 24/13 because only 6 people worked)
$avg{$month2} += $hoursWorked{$name2}{$month2}; } } if ($num == 0) { $avg{$month2} = 0; } else { $avg{$month2} = $avg{$month2}/$num; } } }

Right now, the problem is that it prints the incorrect averages, like 0 instead of 12. I'm sorry if I couldn't explain well, I barely understand what's going on myself. Anyone have any suggestions?

Just Another Perl Wannabe

Replies are listed 'Best First'.
Re: Mathematical averages
by dpuu (Chaplain) on Nov 22, 2002 at 22:41 UTC
    I think your problem is the initial assignment to $num. Your code is confused: that C<for> statement is not a loop, its just an assignment. But the assignment should be inside the month loop, because you are calculating the average per month. A clearer coding of this might be:
    foreach my $month (@calendar) { my $count = 0; my $total = 0; foreach my $value ( grep { $_ != 0 } map { $hoursWorked{$_}{$month} } @members ) { $total += $value; $count ++; } $avg{$month} = $count ? $total/$count : 0; }
    --Dave
Re: Mathematical averages
by dws (Chancellor) on Nov 23, 2002 at 01:00 UTC
    This line     for (my $num = $members) { raises a flag. I would expect to see something like     for my $num ( @members ) { or, if members is a reference to an array,     for my $num ( @$members ) { Instead, what's there is basically a one-trip loop, which leads me to suspect that this isn't what you intended.

Re: Mathematical averages
by arrow (Friar) on Nov 23, 2002 at 04:00 UTC
    Hey, thanks so much dws and dpuu, your answers where just the thing I need. I was surprised at how dpuu's answer worked so well and fast (sorry dws, I just used dpuu's suggesstion first). Anyway, thank you again for saving my butt!

    Just Another Perl Wannabe

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://215265]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-24 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found