sub db_retrieve_records { my $select_query = "SELECT * from records where session=$session"; my $sth = $dbh->prepare($select_query); $sth->execute(); my %sessions; while (@records_results = $sth->fetchrow_array) { my $id = $records_results[0]; $sessions{$id}{id} = $id; $sessions{$id}{action} = $records_results[2]; $sessions{$id}{domain} = $records_results[3]; $sessions{$id}{priority} = $records_results[4]; $sessions{$id}{target} = $records_results[5]; $sessions{$id}{type} = $records_results[6]; $sessions{$id}{session} = $session; } foreach my $key (sort keys %sessions) { push(@loop,\%{$sessions{$key}}); } return \@loop; }