@a=qw(1 0 9 15 6);
@b=qw( 1 1 4 );
####
1 0 9 15 6
1 1 4
__________+
1 1 1 7 0 (which corresponds to the program logic)
####
1 0 9 15 6
1 1 4
__________+
10 9 2 7 0 (which corresponds to the rules of math)
####
#!/usr/local/bin/perl
use strict;
use warnings;
my (@a, @b, @result);
my ($a_len, $b_len,$a, $b, $sum);
@a=qw(1 0 9 15 6); #case1 when @a > @b
@b=qw( 1 1 4 );
#@a = qw( 1 5 9); #case2 when @a < @b
#@b = qw(10 9 3 6);
#case3 when @a == @b
#@a = qw(1 1 6 9 2);
#@b = qw(2 1 4 4 8);
$a_len = @a; #to make decisions on which case to follow
$b_len = @b;
print "@a\n";
print "@b\n";
print "__________+\n";
while(@a || @b){
if($a_len > $b_len){
$a = pop @a;
$b= @b !=0? pop @b : 0; #pop $b or assign zero where empty.
$sum = $a + $b ;
if($sum >= 10 && @a!=0){
$sum -=10; #assign to sum the ones position and strip the tens position.
$a[-1]+=1; # add one to the following position.
}
unshift @result, $sum;
}elsif($a_len < $b_len){
$a= @a!=0? pop @a: 0; # pop $a or assign zero where empty
$b = pop @b;
$sum = $a + $b ;
if($sum >= 10 && @b !=0){
$sum -=10;
$b[-1]+=1;
}
unshift @result, $sum;
}else{
$a = pop @a;
$b = pop @b;
$sum = $a+$b;
if($sum >= 10){
$sum -=10;
$a[-1]+=1;
}
unshift @result, $sum;
}
}
print "@result";