0

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?

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697

2 Answers2

6

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;
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
2

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. ☻

shawnhcorey
  • 3,545
  • 1
  • 15
  • 17