0
@a1 = qw(1 2 3) 
@a2 = qw(1 2 3 4 5)

looking have the resultant of a calculation between a1 and a2 be inserted as the value of a2[0]. example would be 1+1 = 2 going into a2[0] as 2, then the next calculation would be 2+2 (a2[0] + a1[1]) resulting in a2[0] = 4, then 4+3 (a2[0]+a1[2]) resulting in a2[0] = 7, then move on to the next line in a2 and perform the same function against a1.

when all said and done the result would be from print @a2;

7 8 9 10 11

3
  • 1
    so where are you facing the problem in implementing this ?? What's the exact problem you wanted to be address here? Commented Nov 22, 2010 at 5:55
  • I've tried my $a2 = map { my $a = $_; map { $_ + $a } @a1 } @a2; as a nested map but it does not work. I've tired foreach loops but there I can not break the boundry of having to list each item from @a1 foreach (@2) {$_ = $a1[0] + $_;$_ = $a1[1] + $_;$_ = $a1[2] + $_; } Commented Nov 22, 2010 at 5:59
  • not homework, special project for a class, no credit, just something i want to do to understand. Not even a perl class, it's linux and the teacher ran out of stuff to teach. Commented Nov 22, 2010 at 6:03

2 Answers 2

2

So essentially you're adding the total of the values in the first array to each element in the second array.

my $total = 0;
($total += $_) for @a1;
($_ += $total) for @a2;
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, so simple and I was overthinking it way too much. Thank you and this is now solved.
2

Using relevant list functions:

#!/usr/bin/env perl

use strict;
use warnings;

use List::Util      qw( sum   );
use List::MoreUtils qw( apply );

my @a1 = qw( 1 2 3     );
my @a2 = qw( 1 2 3 4 5 );

my $sum = sum(@a1);

@a2 = apply { $_ += $sum } @a2;

Refer:

Also refer Fergal's answer, which is simpler in this case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.