Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re^2: Square the array values.

by kiruthika.bkite (Scribe)
on Mar 18, 2010 at 09:10 UTC ( #829353=note: print w/replies, xml ) Need Help??

in reply to Re: Square the array values.
in thread Square the array values.

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) { $_=$_*$_; }

Replies are listed 'Best First'.
Re^3: Square the array values.
by chromatic (Archbishop) on Mar 18, 2010 at 15:32 UTC

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

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

    ... or even use postfix iteration:

    $_ *= $_ for @array;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://829353]
[Corion]: A good daystart to everybody!
[Corion]: Just a quick poll - is anybody actively relying on https://perlmonks. I plan to retire that URL in favour of moving all our servers onto the same HTTPS certificate for perlmonks.{com, net,org}

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (15)
As of 2017-09-26 07:43 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (292 votes). Check out past polls.