Reputation: 141180
Code
use List::MoreUtils 'pairwise'; # http://stackoverflow.com/a/1865966/54964
my @offset = (0.28)x scalar(@x); # http://www.perlmonks.org/?node_id=110603
my @x = pairwise { $a + $b } @x, @offset;
I would like find a better way to this by default tools.
Is there any better way to do array addition in Perl?
Upvotes: 0
Views: 116
Reputation: 3601
First make it work, then make it better. Or in other words, avoid premature optimization.
my $offset = 0.28;
for my $x_value ( @x ){
$x_value += $offset;
}
Simple means those who have to maintain your code will like you. ☻
Upvotes: 2
Reputation: 118128
There is no need for a pairwise array summation here: That is an issue created your choice to create a second array as big as the original one (at least doubling the memory footprint of your program).
All you are doing is to add a constant to every element of @x
. Use a for
loop:
$_ += 0.28 for @x;
Upvotes: 6