UpTide has asked for the wisdom of the Perl Monks concerning the following question:
I have a 3d matrix and I need to check if several elements equals a value (target value could be different for each element in the future).
Is there any way to simplify the if conditional at the end? I am hoping there would be some way to setup a 'conditional' matrix and compare them while ignoring elements that don't matter in the condition.
currently my code is:
use warnings; use strict; #fancy matrix my @matrix; #set to a blank 3d test matrix for my $i (0 .. 6) { for my $j (0 .. 6) { for my $k (0 .. 2) { $matrix[$i][$j][$k] = 0; } } } #set some values for matrix for my $j (0..2, 4..6) { my $i = 4; for my $k (0 .. 2) { $matrix[$i][$j][$k] = 1; } } for my $i (0..2, 4..6) { my $j = 4; for my $k (0 .. 2) { $matrix[$i][$j][$k] = 1; } } #the conditional problem child if ($matrix[0][4][0] == 1 and $matrix[0][4][1] == 1 and $matrix[0][4][ +2] == 1 and $matrix[1][4][0] == 1 and $matrix[1][4][1] == 1 and $matr +ix[1][4][2] == 1 and $matrix[2][4][0] == 1 and $matrix[2][4][1] == 1 +and $matrix[2][4][2] == 1 and $matrix[4][4][0] == 1 and $matrix[4][4] +[1] == 1 and $matrix[4][4][2] == 1 and $matrix[5][4][0] == 1 and $mat +rix[5][4][1] == 1 and $matrix[5][4][2] == 1 and $matrix[6][4][0] == 1 + and $matrix[6][4][1] == 1 and $matrix[6][4][2] == 1 and $matrix[4][0 +][0] == 1 and $matrix[4][0][1] == 1 and $matrix[4][0][2] == 1 and $ma +trix[4][1][0] == 1 and $matrix[4][1][1] == 1 and $matrix[4][1][2] == +1 and $matrix[4][2][0] == 1 and $matrix[4][2][1] == 1 and $matrix[4][ +2][2] == 1 and $matrix[4][4][0] == 1 and $matrix[4][4][1] == 1 and $m +atrix[4][4][2] == 1 and $matrix[4][5][0] == 1 and $matrix[4][5][1] == + 1 and $matrix[4][5][2] == 1 and $matrix[4][6][0] == 1 and $matrix[4] +[6][1] == 1 and $matrix[4][6][2] == 1) { print "it worked"; } <stdin>;
ps: yes this is a shameless crosspost from stackoverflow
|
|---|