Another variation, also on the shoulders of others:
Using an intermediate 'convenience' variable
my $array_ref = $matrix{NI};
to hold the reference of interest can greatly simplify/clarify matters. It doesn't buy you much in this particular case, but can be very helpful with complex, deeply nested data structures.
>perl -wMstrict -le
"use List::Util qw(min);
use List::MoreUtils qw(indexes);
;;
my %matrix = ( NI => [ 1, 5, 1, 3, 4 ] );
;;
my @indices = do {
my $array_ref = $matrix{NI};
my $mat_min = min @$array_ref;
indexes { $_ == $mat_min } @$array_ref;
};
print qq{(@indices)};
"
(0 2)
Update: And, of course, the guts of a do { ... } block can usually be easily lifted out and plunked down in a subroutine:
>perl -wMstrict -le
"use List::Util qw(min);
use List::MoreUtils qw(indexes);
;;
my %matrix = (
foo => { bar => [ 'oops', { NI => [ 1, 5, 1, 3, 4 ], }, ], },
);
;;
my @indices = get_indices($matrix{foo}{bar}[1]{NI});
print qq{(@indices)};
;;
sub get_indices {
my ($ar) = @_;
;;
my $mat_min = min @$ar;
return indexes { $_ == $mat_min } @$ar;
}
"
(0 2)
|