in reply to Storing 10 numbers in array
There were several errors, but the main one was declaring a second @ten_array inside the loop. Others were:
A cleaned up version of your code with a few other changes is shown below. Note in particular the use of strict (always use strictures) and a Perl for loop to count the loop iterations instead of hand roling a while loop to do that.
#!/usr/bin/perl use strict; use warnings; my $wanted = 3; my @numbers; print "Enter $wanted numbers.\n"; for (1 .. $wanted) { my $number = <>; chomp($number); push @numbers, $number; square($number); } print "The ten numbers were @numbers\n"; sub square { my ($number) = @_; my $square_num = $number * $number; print "$number squared is: $square_num\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Storing 10 numbers in array
by CountZero (Bishop) on Mar 25, 2012 at 20:39 UTC | |
|
Re^2: Storing 10 numbers in array
by snakeyes52 (Initiate) on Mar 25, 2012 at 21:41 UTC |