Search code examples
arraysperlscalar

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


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!


Solution

  • Use foreach.

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