print "Please enter a number: \n";
my ($limit)= <STDIN>;
chomp $limit;
my $sum = 1;
my @array = ();
for (my $i=1; $i<=$limit; $i+=2)
{
$sum += 2
push(@array,'1')
}
my $average = $sum/scalar(@array);
print "$average\n";
-
Syntax terror: there's a semicolon missing between $sum += 2 and push(@array,'1'). Most of us would end push(@array,'1'); with a semicolon, too.
-
Context: my ($limit) indicates "list context", which forces you to explicitly enter an EOF after your input. A very good explanation to this topic is in Modern Perl by chromatic, here, but I recommend reading the complete book, eventually.
Here, it should be my $limit = <STDIN>; without the parens.
-
Algorithm: You are computing the average of n times 2, and you calculate it wrong.
I recommend that you put
print "i: $i, sum: $sum, array: @array, scalar: ", scalar(@array), "\n
+";
after the push (which means you need the aforementioned semicolon)
to see what is going on:
-
The sum has to start with zero, not with one.
-
You have to add $i, not a constant of two.
-
You should push $i instead of '1'