snakeyes52 has asked for the wisdom of the Perl Monks concerning the following question:

This program asks for 10 numbers, stores them in an array, and calculates their squares. My intention is for program to print the array of the 10 entered numbers as the last line. If fails to do this, however. Any suggestions?

#!/usr/bin/perl -w $count=1; my @ten_array=0; while($count<=10) { print "Enter ten numbers.\n"; $number=<>; chomp ($number); my @ten_array=($number); ++$count; square($number); sub square { $square_num =$number*$number; print"$number squared is: $square_num\n"; } } print"The ten numbers were @ten_array\n";

Replies are listed 'Best First'.
Re: Storing 10 numbers in array
by GrandFather (Saint) on Mar 25, 2012 at 20:12 UTC

    There were several errors, but the main one was declaring a second @ten_array inside the loop. Others were:

    1. Initialising @ten_array with 0 which doesn't clear the array as I guess you were thinking, but assigns 0 as the first element
    2. Declaring square nested. It will not work as you expect
    3. Assigning $number to @ten_array. Even if you hand't redeclared @ten_array that wouldn't have worked because it simply replaces any previous content of the array with a single element containing $number.
    4. You use $number as a global inside square, but you call square passing a value into it. Usage and implementation are not consistent.

    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"; }
    True laziness is hard work
      Nitpicking, but to make your program perfect
      print "The ten numbers were @numbers\n";
      should be
      print "The $wanted numbers were @numbers\n";

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics

      Many thanks for your enlightened solution!

Re: Storing 10 numbers in array
by moritz (Cardinal) on Mar 25, 2012 at 20:05 UTC

    You declare a new variable @ten_array inside the loop, and assign one value to it. So the outer variable @ten_array (which really is a different variable) never stores any interesting value.

    You should just push onto the outer variable.

    See also: Coping with Scoping.

      all in the scope snakeyes

Re: Storing 10 numbers in array
by Marshall (Canon) on Mar 26, 2012 at 01:36 UTC
    I would suggest something like this:
    #!/usr/bin/perl -w use strict; use Data::Dumper; my @numbers; foreach my $number (1..3) { my $num = prompt_4_number("Enter number $number of 3"); push @numbers, $num; } print Dumper \@numbers; sub prompt_4_number { my $prompt = shift; my $number; while ( (print "$prompt: "), $number = <STDIN> ) { next if $number =~ /^\s*$/; # re-prompt on blank lines $number =~ s/^\s*//; # delete leading spaces $number =~ s/\s*$//; # delete trailing spaces if ( $number !~ /^\s*((-?\d*)(\.\d*)?)\s*$/) # not a valid # floating point # number { print " Not a valid decimal number!\n"; next; # causes a re-prompt } return $number; } } __END__ C:\TEMP>perl get_nums.pl Enter number 1 of 3: afd Not a valid decimal number! Enter number 1 of 3: 3.4 Enter number 2 of 3: .5.7.8 Not a valid decimal number! Enter number 2 of 3: 5.7 Enter number 3 of 3: 5Avsx Not a valid decimal number! Enter number 3 of 3: 10 $VAR1 = [ '3.4', '5.7', '10' ];