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;