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; } #### Row 0 has a max of 9 in column(s) 2, 5 Row 1 has a max of 7 in column(s) 7, 9 Row 2 has a max of 7 in column(s) 6 Row 3 has a max of 9 in column(s) 1 Row 4 has a max of 9 in column(s) 3 Row 5 has a max of 9 in column(s) 8 Row 6 has a max of 6 in column(s) 5, 6, 7, 8 Row 7 has a max of 9 in column(s) 7 Row 8 has a max of 8 in column(s) 0 Row 9 has a max of 9 in column(s) 0