use strict;
use warnings;
my ($i, $j, $x, $tot);
my @exes;
print "Please enter for how many values of x you will be finding the
Standard Deviation.\n";
do {chomp($i=<>); print "\nPlease only use numbers.\n" if $i=~m/\D/;}
until($i!~m/\D/ and $i=~m/\d/);
print "\nPlease input the values of x, one at a time.\n";
for $j(1..$i){
do {chomp($x=<>); print "\nPlease only use numbers.\n" if $x=~m/\D/;}
until($x!~m/\D/ and $x=~m/\d/);
push @exes, $x;}
undef $x;
for(@exes){
$x=$x+$_;}
my $ave=$x/$i;
for(@exes){
$tot=$tot+(($_-$ave)^2);}
my $deviation=sqrt($tot/$i);
print "\nThe Standard Deviation of that set of numbers is $deviation.\n";
Update: Nevermind...obviously this is not the problem here (and obviously I was not intimately familiar with the default STDIO buffering behaviour, and I haven't read that link myself for a while).
it won't tell me what's wrongInterestingly, when I tried to run it, it did.
Maybe you should use do {stuff;} until (condition()); rather than {stuff;} until (condition());syntax error at a.pl line 8, near ");" syntax error at a.pl line 12, near ");" Global symbol "$n" requires explicit package name at a.pl line 20. Execution of a.pl aborted due to compilation errors.
use strict;
use warnings;
my ($i, $j, $x, $tot);
my @exes;
print "Please enter for how many values of x you will be
finding the Standard Deviation.\n";
do {chomp($i=<>); print "\nPlease only use numbers.\n" if $i=~m/\[1-9]/;}
until($i!~m/\[1-9]/);
print "\nPlease input the values of x, one at a time.\n";
for $j(1..$i){
do {chomp($x=<>); print "\nPlease only use numbers.\n" if $x=~m/\[1-9]/;}
until($x!~m/\[1-9]/);
push @exes, $x;}
undef $x;
for(@exes){
$x=$x+$_;}
my $ave=$x/$i;
for(@exes){
$tot=$tot+(($_-$ave)^2);}
my $deviation=sqrt($tot/$n);
print "\nThe Standard Deviation of that set of numbers is $deviation.\n";
Once the until statements are fixed, and a my $i; is added, you have a divide by zero in
my $deviation=sqrt($tot/$i);. How are you executing this, if it is a *nix you need a #! line, if win32, ther could be a problem with your install. Thank you
UPDATE: removed the my $i, the line my $deviation=sqrt($tot/$n); changed to my $deviation=sqrt($tot/$i); from the first time I downloaded until I looked again.
UPDATE: fixed the formating errors of the firs update...
Wow, that is one messy chunk of code you have. A long time ago, [Ovid] wrote this gem: [id://171322]. Please consider using tools such as [http://perltidy.sourceforge.net/|perltidy] to make your code more readable ... for others. Here is what your code looks like after i ran it through perltidy (i'm using the code you orginally posted as saved by [betterworld]):
use strict;
use warnings;
my ( $i, $j, $x, $tot );
my @exes;
print "Please enter for how many values of x you will be finding the Standard Deviation.\n";
do {
chomp( $i = <> );
print "\nPlease only use numbers.\n" if $i =~ m/\[1-9]/;
} until ( $i !~ m/\[1-9]/ );
print "\nPlease input the values of x, one at a time.\n";
for $j ( 1 .. $i ) {
do {
chomp( $x = <> );
print "\nPlease only use numbers.\n" if $x =~ m/\[1-9]/;
} until ( $x !~ m/\[1-9]/ );
push @exes, $x;
}
undef $x;
for (@exes) {
$x = $x + $_;
}
my $ave = $x / $i;
for (@exes) {
$tot = $tot + ( ( $_ - $ave ) ^ 2 );
}
my $deviation = sqrt( $tot / $n );
print "\nThe Standard Deviation of that set of numbers is $deviation.\n";
I hope you will agree that posting code that is easier to read by others will help you receive better answers, faster (not that the fast answers you have already received are bad). Help us help you.
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
If it does, and I bet it will, it would be a good idea to set
$| = 1;near the top of your script.
Furthermore, your gives a few warnings, caused by
undef $x;
for(@exes){
$x=$x+$_;
}
which can be fixed by eiter setting $x to 0 instead, or by doing
undef $x;
for(@exes){
$x += $_;
}
which does the same thing, but is shorter, and doesn't warn if $x is undef.
Worse, there's a nasty error, that makes your script output a wrong result: the power operator in Perl is **, not ^, which does XOR. See [perlop]. So:
for(@exes){
$tot += ($_-$ave)**2;
}
perlmonks.org content © perlmonks.org and Andrew_Levenson, bart, betterworld, chromatic, gawatkins, jeffa, Popcorn Dave, runrig
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03