Refusal to Run
Andrew_Levenson
created: 2006-08-02 16:16:12
I have some code, a simple test program to get me re-acquainted with the syntax and all after a break.
However, my computer absolutely refuses to run it. My command prompt locks up. It's nothing tasking or anything, and it won't tell me what's wrong. Any ideas?
Edit: Fixed using ideas from the CB, and it still won't start. :(
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";


Thanks.
Re: Refusal to Run
created: 2006-08-02 16:24:28
Suffering from Buffering maybe?

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).

Re^2: Refusal to Run
created: 2006-08-02 16:45:57

Why would that be? What evidence is there that the OP has changed the default STDIO buffering in such a way that this would be a problem?

Re: Refusal to Run
created: 2006-08-02 16:32:58
it won't tell me what's wrong
Interestingly, when I tried to run it, it did.
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.
Maybe you should use do {stuff;} until (condition()); rather than {stuff;} until (condition());

Since the OP has updated his code, this post has become futile. For reference, here is the original code:
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";
Re: Refusal to Run
created: 2006-08-02 16:50:47

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
Greg W

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...

Re: Refusal to Run
created: 2006-08-02 17:30:36

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)
Re^2: Refusal to Run
created: 2006-08-02 21:01:06
Wow, thanks, that IS a lot more readable.
I'll fix mine up and re-post it tonight when I get the chance.

And thanks for being congenial about it.
Re: Refusal to Run
created: 2006-08-02 23:55:19
Have you tried stepping through it with a debugger to see exactly where it's hanging? My debugger of choice is ptkdb but YMMV.

Revolution. Today, 3 O'Clock. Meet behind the monkey bars.
Re: Refusal to Run
created: 2006-08-03 07:35:17
I'm sorry to spoil your party, but your script works for me. Like I already said in the Chatterbox, it probably is just waiting for you to enter data. Even if you don't see any prompts, try entering numbers followed by return at the console, and see if it responds to it.

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;
}
Re^2: Refusal to Run
created: 2006-08-03 11:52:39
It is not waiting for input, I thought of that.
It just sits there. If it runs for everyone else, I figure it's my machine, not the script.

And thanks for reminding me about the exponentation bit, I completely forgot.

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