in reply to 36 Conditions in If Statement [sendhelp]]

Hello UpTide,

Here’s an approach which, although less efficient, is easier to read and therefore to debug:

use strict; use warnings; use List::Util qw( all ); ... # create and populate my @matrix as before ... my @indices = ( [0, 4, 0], [0, 4, 1], [0, 4, 2], [1, 4, 0], [1, 4, 1], [1, 4, 2], [2, 4, 0], [2, 4, 1], [2, 4, 2], [4, 4, 0], [4, 4, 1], [4, 4, 2], # A [5, 4, 0], [5, 4, 1], [5, 4, 2], [6, 4, 0], [6, 4, 1], [6, 4, 2], [4, 0, 0], [4, 0, 1], [4, 0, 2], [4, 1, 0], [4, 1, 1], [4, 1, 2], [4, 2, 0], [4, 2, 1], [4, 2, 2], [4, 4, 0], [4, 4, 1], [4, 4, 2], # B [4, 5, 0], [4, 5, 1], [4, 5, 2], [4, 6, 0], [4, 6, 1], [4, 6, 2], ); my @to_test; push @to_test, $matrix[ $_->[0] ][ $_->[1] ][ $_->[2] ] for @indices; print "It worked\n" if all { $_ == 1 } @to_test;

By making it easier to see which array elements are being tested, this approach reveals that three of the tests are in fact duplicated (see the lines labelled A and B).

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: 36 Conditions in If Statement [sendhelp]
by AnomalousMonk (Archbishop) on Feb 04, 2017 at 17:58 UTC

    The approach you suggest is very ++nice. It can easily be adapted to other cases; e.g., a case in which the comparison may vary with each tuple of indices.

    use strict; use warnings; use List::Util qw(all); # create and populate my @matrix as before ... my @tuples = ( [1, 0, 4, 0], [2, 0, 4, 1], [3, 0, 4, 2], [4, 1, 4, 0], [5, 1, 4, 1], [6, 1, 4, 2], ... [9, 4, 6, 0], [8, 4, 6, 1], [7, 4, 6, 2], ); do_something() if all { $_->[0] == $matrix[$_->[1]][$_->[2]][$_->[3]] +} @tuples;
    At a slight performance cost, the subroutine block of  all could be made a bit more readable:
        { my ($n, $i, $j, $k) = @$_;  $n == $matrix[$i][$j][$k]] }


    Give a man a fish:  <%-{-{-{-<