Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Returning indices with same values for array within hash.

by AnomalousMonk (Monsignor)
on Nov 10, 2013 at 17:00 UTC ( #1061914=note: print w/ replies, xml ) Need Help??


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

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)


Comment on Re: Returning indices with same values for array within hash.
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1061914]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2014-07-25 04:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (167 votes), past polls