No offense, but I think a basic lesson in debugging is applicable here:
- Is $sum actually 0 when you start (we're pretty sure that my $sum; would set it to 0, but I never trust that kind of thing.)
- Have you printed the values out as you go? For each loop iteration, printing out the value of all variables involved would prove to be useful.
- Is the algorithm correct? You're recalculating the $std value everytime through the loop. I suck at math, but is this correct? Using the code you have there, I get the exact same result.
I'll give you a hint: You're dividing by $avg, but I don't see it being set anywhere. Do you?
--Chris
e-mail jcwren
| [reply] [d/l] |
use strict;
my $sum;
unless (defined($sum)) {
print '$sum is undefined', "\n";
} else {
print 'The value of $sum is ', "$sum\n";
}
This program returns the message that $sum is undefined.
| [reply] [d/l] [select] |
At the risk of sounding redundant, wouldn't perl -w catch
the use of $avg when undefined, thereby making
the debugging easier? All the debug tips are great, but the
best one is letting perl debug for you with
use strict; and -w.
On a stylistic note, you might want to scope your variables
like this:
my ($a, $b, $c, $d);
Instead of this:
my $a;
my $b;
my $c;
my $d;
This should help keep your code from getting diluted with
relatively less important text. (The computation is
more important than the declarations, so it should also take
up more space on screen to help focus your mind.)
-Ted | [reply] [d/l] [select] |
How about posting part of your dataset? We really have no idea what to expect without knowing what the input is.
--Chris
e-mail jcwren
| [reply] |
The pa5c.DAT contains the following:
Smith:70
Jones:74
Rider:80
High:82
Billie:68
Smithsonville:76
| [reply] [d/l] |