use Modern::Perl; use List::Util qw( max ); # Build a sample dataset of ten rows of ten columns each, # with random numbers between zero and nine. my @lists = map { [ map { int( rand( 10 ) ) } 1 .. 10 ] } 1 .. 10; # Invoke our dirty little function to get the list # of maxes per row, and columns that hit that max. my @max_indices = get_max_and_column_list_per_row( @lists ); # Print our list out nicely. { local $" = ", "; foreach my $row_index ( 0 .. $#max_indices ) { my ( $max, $columns ) = each %{ $max_indices [ $row_index ] }; say "Row $row_index has a max of $max in column(s) @{$columns}"; } } # The ugly function. Accepts a LoL, returns a LoHoL. sub get_max_and_column_list_per_row { my( @arrays ) = @_; # First, find out how many columns we have. my $top_index = max( map $#{$_}, @arrays ); # The rest is a big map{}. return map { my $outer_index = $_; my $current_max = max( @{ $lists[$outer_index] } ); +{ # Anonymous hash constructor. $current_max => [ grep { $lists[$outer_index][$_] == $current_max } 0 .. $top_index ] } # End of anonymous hash constructor. } 0 .. $#lists; }