in reply to Returning indices with same values for array within hash.

All solutions so far have two passes over the array, but it can be done in one as well. I am using each, so you need one of the newer Perls.

use strict; use warnings; my %matrix = (NI => [2, 1 ,1 ,3 ,4] ); my @NI_index; my $min; while( my ($i, $v) = each @{$matrix{NI}} ) { if( not defined $min or $v < $min ) { $min = $v; # found new minimum @NI_index = (); # empty list } push @NI_index, $i if $v == $min; } print "@NI_index\n";

Replies are listed 'Best First'.
Re^2: Returning indices with same values for array within hash.
by AnomalousMonk (Archbishop) on Nov 11, 2013 at 17:12 UTC
    ... each ... newer Perl ...

    Perl version 5.12+, and 5.14+ has the "highly experimental" feature of allowing each to directly take a reference:

    >perl -wMstrict -le "my %matrix = (NI => [2, 1, 1, 3, 4] ); ;; my @NI_index; my $min; while (my ($i, $v) = each $matrix{NI}) { if(not defined $min or $v < $min) { $min = $v; @NI_index = (); } push @NI_index, $i if $v == $min; } print qq{@NI_index}; " 1 2