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

perl map array of hashes
perl operate on each element of array
perl remove character from array
perl foreach array reference
perl array reference
perl last element of array
foreach loop in array in perl
perl array to hash

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!


Use foreach.

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

🤦 How can I multiply each element of an array by a scalar in perl , Use foreach. foreach my $x (@array) { $x = $x * $scalar; }. An array is a variable that stores an ordered list of scalar values. Array variables are preceded by an "at" (@) sign. To refer to a single element of an array, you will use the dollar sign ($) with the variable name followed by the index of the element in square brackets.


You can try this:

@array = map { $_ * $scalar } @array;

or more simply:

map { $_ *= $scalar } @array;

multiplying array by constant - Perl, There's no way other than multiplying individually each element by the constant. You can write it more or less graciously, you can write a routine  Technically there is no such thing as a multidimensional array in Perl but arrays are used to act as they have more than one dimension. Multi dimensional arrays are represented in the form of rows and columns, also knows as matrix. Each element of an array can be a reference to another array but in syntax, they will appear like a 2-dimensional


Howabout this:

foreach(@array)
{ $_ *= $scalar }

As you see, you can modify the array in-place as it's traversed.

array elements multiplication with another array , I would like to multiply the array elements of @array1 with @array2 elements. When your only tool is a hammer, all problems look like your thumb. Perlintro (​Variable Types) in conjunction with Arrays: A Tutorial/Reference  Defining and using multidimensional arrays is not much harder; but before we look at how to do it, you need to understand array references in Perl. References to Arrays in Perl. Take a look at the following code. If you can understand the following, you basically know all you need to know about array references in Perl. References are scalar


I don't know the scope of your need. IFF you are doing numerical data manipulation, the Perl Data Language (PDL) takes an array of numerical data, creates a "piddle" object from it and overloads mathematical operations to "vectorize" their operation. This is a very efficient system for doing numerical processing. Anyway here is an example:

#!/usr/bin/perl

use strict;
use warnings;

use PDL;

my $pdl_array = pdl([1,1,2,3,5,8]);
print 2*$pdl_array;

__END__
gives:
[2 2 4 6 10 16]

Doing Something with Every Element in a List, Often you use an array to collect information you're … The variable set to each value in the list is called a loop variable or iterator variable. @array = (1,2,3); foreach $item (@array) { $item--; } print "@array\n"; 0 1 2 # multiply everything in​  Technically speaking there are no multi-dimensional arrays in Perl, but you can use arrays in Perl to act as if they had more than one dimension. In Perl each element of an array can be a reference to another array, but syntactically they would look like a two-dimensional array.


This comment is for SoloBold.

Here is a test of the map approach:

#!/usr/bin/perl                                                                                                                                                                                                          

use strict;
use warnings;
use Benchmark;

my @array = ();
push(@array, (1) x 1000000);
my $scalar = 5;

my $startTime = new Benchmark();

@array = map { $_ * $scalar } @array;

my $stopTime = new Benchmark();

print STDOUT "runtime: ".timestr(timediff($stopTime, $startTime), 'all')." sec\n";

Here is a test of the foreach approach:

#!/usr/bin/perl                                                                                                                                                                                                          

use strict;
use warnings;
use Benchmark;

my @array = ();
push(@array, (1) x 1000000);
my $scalar = 5;

my $startTime = new Benchmark();

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

my $stopTime = new Benchmark();

print STDOUT "runtime: ".timestr(timediff($stopTime, $startTime), 'all')." sec\n";

Here is the system I'm running on:

bash-3.2$ perl --version
This is perl, v5.8.8 built for darwin-2level
...
bash-3.2$ uname -a
Darwin Sounder.local 10.7.0 Darwin Kernel Version 10.7.0: Sat Jan 29 15:17:16 PST 2011; root:xnu-1504.9.37~1/RELEASE_I386 i386

Here were results from one test:

bash-3.2$ ./test.map.pl
runtime:  4 wallclock secs ( 0.41 usr  0.70 sys +  0.00 cusr  0.00 csys =  1.11 CPU) sec
bash-3.2$ ./test.foreach.pl
runtime:  0 wallclock secs ( 0.13 usr  0.00 sys +  0.00 cusr  0.00 csys =  0.13 CPU) sec

These times are fairly reproducible on the same machine, and the results are somewhat repeatable on a dual-core Linux box:

[areynolds@fiddlehead ~]$ perl --version
This is perl, v5.8.8 built for x86_64-linux-thread-multi
...
[areynolds@fiddlehead ~]$ uname -a
Linux fiddlehead.example.com 2.6.18-194.17.1.el5 #1 SMP Mon Sep 20 07:12:06 EDT 2010 x86_64 GNU/Linux
[areynolds@fiddlehead ~]$ ./test.map.pl
runtime:  0 wallclock secs ( 0.28 usr  0.05 sys +  0.00 cusr  0.00 csys =  0.33 CPU) sec
[areynolds@fiddlehead ~]$ ./test.foreach.pl
runtime:  0 wallclock secs ( 0.09 usr  0.00 sys +  0.00 cusr  0.00 csys =  0.09 CPU) sec

The ratio of performance on the OS X box is 8.53x slower for map versus foreach. On the Linux box, 3.67x slower for the same.

My Linux box is dual-core and has a slightly faster cores than my single-core OS X laptop.

EDIT

I updated Perl from v5.8.8 to v5.12.3 on my OS X box and got a considerable speed boost, but map still performed worse than foreach:

sounder:~ alexreynolds$ perl --version
This is perl 5, version 12, subversion 3 (v5.12.3) built for darwin-multi-2level
...
sounder:~ alexreynolds$ ./test.map.pl
runtime:  0 wallclock secs ( 0.45 usr  0.08 sys +  0.00 cusr  0.00 csys =  0.53 CPU) sec
sounder:~ alexreynolds$ ./test.foreach.pl
runtime:  1 wallclock secs ( 0.18 usr  0.00 sys +  0.00 cusr  0.00 csys =  0.18 CPU) sec

This goes from 8.53x worse to 2.94x worse. A fairly substantial improvement.

The Linux box performed slightly worse with upgrading its Perl installation to v5.12.2:

[areynolds@basquiat bin]$ perl --version    
This is perl 5, version 12, subversion 2 (v5.12.2) built for x86_64-linux-thread-multi
...
[areynolds@basquiat bin]$ /home/areynolds/test.map.pl
runtime:  1 wallclock secs ( 0.29 usr  0.07 sys +  0.00 cusr  0.00 csys =  0.36 CPU) sec
[areynolds@basquiat bin]$ /home/areynolds/test.foreach.pl
runtime:  0 wallclock secs ( 0.08 usr  0.00 sys +  0.00 cusr  0.00 csys =  0.08 CPU) sec

This goes from 3.67x worse to 4.5x worse — not so good! It might not always pay to upgrade, just for the heck of it.

Perl 6 Now: The Core Ideas Illustrated with Perl 5, Bad values occupy individual positions in an array. Arrays are composed of elements, and each element is the same size. the memory requirements of a PDL array are easy to compute: multiply each of the dimensions together, and then  The map() method creates a new array with the results of calling a provided function on every element in the calling array. In my opinion, .map() is more suitable if someone wants to create a new array based on input values from the current array. However, if someone wants to modify the array in place, .forEach() seems a better choice. In ES6


4. Arrays - Perl Cookbook, 2nd Edition [Book], The scalar items in an array or list are called elements, which you access by placing an “and” before the last element and commas between each element if foreach $item (@array) { $item--; } print "@array\n"; 0 1 2 # multiply everything in​  Just as a note, operations on arrays, like scalar multiplication are highly optimized in numpy, and are significantly faster than list comprehensions. It is generally advisable to not treat numpy arrays like python lists. – JoshAdel Nov 19 '11 at 16:20


Recipe 2.14. Multiplying Matrices (Perl Cookbook), Alternatively, apply the matrix multiplication algorithm to your two-dimensional array: sub mmult { my ($m1,$m2) = @_; my ($m1rows,$m1cols) = matdim($m1);  Hence if we pass an array as a parameter, that array will be placed in SCALAR context and it will return the number of elements in it. In our example the array had 6 elements thus the expression say length @many_strings; was converted to say length 6; and the length of the number 6 is the same as the length of the string "6" which is 1.


Beginning Perl, If we want to double every value in an array, then for each element we come across, we multiply by 2. The keyword to use here is foreach. The foreach loop has  There's no way other than multiplying individually each element by the constant. You can write it more or less graciously, you can write a routine that does it changing the content of the array, there is also a Matrix module (but personally wouldn't even look at it for such a simple task), but that's it: no matrix manipulation instructions available with perl (as with almost all languages).