I have the following script:
#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw(sum);
my @a1=([1,2,3],
[4,5,6],
[7,8,9]);
my @a2=([4,3],
[2,1]);
my @subtracted;
for (my $i=0; $i<=$#a2; $i++){
for (my $j=0; $j<=$#a2; $j++){
my $subtraction=$a1[$i][$j] - $a2[$i][$j];
print "$subtraction\n";
my $abs=abs($subtraction);
push(@subtracted, $abs);
}
}
my $sum=sum(@subtracted);
my $average=$sum/@subtracted;
print "Average=$average\n";
exit;
This prints:
-3
-1
2
4
Average=2.5
This is subtracting the following referenced elements:
$a1[0][0] - $a2[0][0]
$a1[0][1] - $a2[0][1]
$a1[1][0] - $a2[1][0]
$a1[1][1] - $a2[1][1]
But now I need it to do:
$a1[1][1] - $a2[0][0]
$a1[1][2] - $a2[0][1]
$a1[2][1] - $a2[1][0]
$a1[2][2] - $a2[1][1]
and print:
1
3
6
8
Average=4.5
So that the elements of @a2 are fixed but the elements of @a1 are moved 1 line and 1 column (the number of compared elements is always limited by the number of elements in @a2). But now I am completely stuck and have no idea of how to do it. So, thank you in advance for your ideas.
