my @tabular = $dbi->array_of_hashrefs_please(); # move into a hash (keyed by category) of array of hashrefs (rows) my %categories; foreach( @tabular ) { my $entry = $_->{ FIELD_I_WANT_TO_SORT_BY }; if ( $categories{$entry} == undef ) { $categories{$entry} = [$_] ; } else { push @{$categories{$entry}}, $_; } } # now create nested loop based on category... my @nested_loop; foreach(sort keys %categories) { push @nested_loop, { category => $_, entries => $categories{$_}}; } # add our nested loop to the template $template->param( my_loop => \@nested_loop); ####