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!
it seem unfortunate to me that Larry didn't allow
$scalar operator (list)
or
(list) operator $scalar
Sure map or loops can do it, but the syntax is so much cleaner like above.
Also (list) operator (list)
makes sense too if the 2 are equal length.
Surprised Larry didn't allow these, just saying.. I guess in this case there were (n-1) ways to do it.
Like
my @a = 'n' . (1..5); my @a = 2 * (1..5);
or even my @a = 2 * @b;
You can try this:
@array = map { $_ * $scalar } @array;
or more simply:
map { $_ *= $scalar } @array;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With