Reputation: 321
I'm working on a model for predicting disease outcomes and had it working until I started trying to create user prompts. The code stops at the "duration" prompt, located at the bottom of this code block. The script just seems to freeze (blinking cursor) and doesn't go anywhere, but there is 100% processor use so it's doing something. I tried printing text and it seems to stop working at the "duration" prompt. The entire code is on GitHub if you feel like trying to run it yourself.
I've looked it over quite a few times and can't see anything wrong anywhere and am hoping some fresh eyes can see something. I'm sure I made a dumb mistake somewhere. Thanks much!
EDIT: If I remove all the code after "my %population," it works.
#!/usr/bin/perl
use 5.10.1;
#use strict;
use warnings;
use Data::Dumper;
use Storable qw(dclone);
print "Enter number of individuals: ";
my $NUM_IND = <STDIN>;
exit 0 if ($NUM_IND eq "");
print "Enter initial number of infections: ";
my $INIT = <STDIN>;
exit 0 if ($INIT eq "");
print "Enter number contacts per individual: ";
my $CONTACT_RATE = <STDIN>;
exit 0 if ($CONTACT_RATE eq "");
print "Enter disease infectious period: ";
my $INFECTIOUS_PERIOD = <STDIN>;
exit 0 if ($INFECTIOUS_PERIOD eq "");
print "Enter disease virulence: ";
my $INFECTIVITY = <STDIN>;
exit 0 if ($INFECTIVITY eq "");
print "Enter disease incubation period: ";
my $INCUB = <STDIN>;
exit 0 if ($INCUB eq "");
print "Enter number of vaccinations per day: ";
my $VAC = <STDIN>;
print "Enter vaccine efficacy: ";
my $EF = <STDIN>;
print "Enter duration of model: ";
my $DURATION = <STDIN>;
exit 0 if ($DURATION eq "");
print "this works";
my %population = ();
Upvotes: 0
Views: 114
Reputation: 126722
Please never comment out use strict
. It is probably worse than not including it in the first place. strict
is a huge help with finding bugs in your programs and should be embraced, not avoided.
The reason you are not seeing this works
is because the output buffer isn't being flushed. If you add
STDOUT->autoflush;
at the top of your program (say, after the use
statements) then it will force the buffer to be flushed after every print
statement and you will see your text.
The real problem is this line
for (my $i = 0 ; $i = $INIT ; $i++) {
where your condition is the assignment $i = $INIT
which will always be true as long as $INIT
is non-zero.
I assume you want $i < $INIT
, like the other loops.
But it is far better to use Perl's range operator, which is much clearer to read and isn't vulnerable to mistakes like this. Just use
for my $i (0 .. $INIT-1) { ... }
and it will be obvious what is happening.
Upvotes: 2