How do I multiply each member of array by a scalar in perl?

nebulus picture nebulus · Apr 15, 2011 · Viewed 11.7k times · Source

Here is the code...

use strict;
use warnings;

my @array= (1,2,3,4,5);
my $scalar= 5;

@array= $scalar*@array;

print @array;

Need something that can perform similar function with little code. Thanks!

Answer

RC. picture RC. · Apr 15, 2011

Use foreach.

foreach my $x (@array) { $x = $x * $scalar; }