in reply to Square the array values.

Somehow you have to loop through the array and modify the values in place with the exponentiation operator (or the multiplication operator). perlsyn describes most of the interesting parts in the section on "Foreach Loops".

Replies are listed 'Best First'.
Re^2: Square the array values.
by kiruthika.bkite (Scribe) on Mar 18, 2010 at 09:10 UTC
    Is this you are expecting.
    use strict; use warnings; use Data::Dumper; my @array=(1,2,3); print Dumper \@array; my $len=$#array; while($len>=0) { $array[$len]=$array[$len]*$array[$len]; $len--; } print Dumper \@array;


    Or use the following way.
    my @array=(1,2,3); foreach (@array) { $_=$_*$_; }

      The latter is much better, though you could multiply and assign in place with =*:

      for (@array) { $_ *= $_; }

      ... or even use postfix iteration:

      $_ *= $_ for @array;