0

I have some scalar values in array

@array=(1,2,3,4,5);

We can directly assign these values to variables as

($a,$b,$c,$d,$e)=@array;

Is there some way so that I can add the corresponding values of @array numbers like $x +=10;

($a,$b,$c,$d,$e) +=@array;

Sorry for asking such silly question ;)

2 Answers 2

1

Try using map

my @array=(1,2,3,4,5);
my ($a,$b,$c,$d,$e) = map { $_ + 10 } @array;
Sign up to request clarification or add additional context in comments.

Comments

0

You can sum all elements of array using sum from List::Util:

use List::Util qw(sum);

my $sum = sum(@array);

UPDATE: It seems that you want to add arrays element by element, then you can use pairwise from List::Moreutils:

use List::MoreUtils qw(pairwise);

my @array = qw(10 20 30);
my @incr  = qw( 1  2  3);
pairwise { $a += $b } @array, @incr;   # (11,22,33)

3 Comments

I don't want sum of all elements in array instead I need the values of the variables $a,$b,$c.. to be increased by corresponding values in array
or just my $i = 0; map {$_+=$array[$i]; $i++;} ($a, $b, $c, $d, $e);
@Suic thanks n +1 that is the solution that i am using...as I don't have to reassign the values to the variable

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.