foreach my $row (@data) { push @{$hash{$row->[0]}}, { companyid => $row->[1], apptype => $row->[2], state => $row->[3], count => $row->[4], }; } #### %hash = ( 'One' => [ { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' }, { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' }, { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' } ], 'Two' => [ { 'apptype' => 'xxx', 'count' => '123', 'companyid' => 'xxx', 'state' => 'xx' }, ] ); #### foreach my $rep (sort keys %hash) { $template->param( REP => { rep => $rep, DATA => $hash{$rep} }, ); } ####