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


In reply to 36 Conditions in If Statement [sendhelp]] by UpTide

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.