my $hash_ref1 = $dbh->selectall_hashref( $sql1, $key_field1, {}, @values1 ); my $hash_ref2 = $dbh->selectall_hashref( $sql2, $key_field2, {}, @values2 ); my $hash_ref3 = $dbh->selectall_hashref( $sql3, $key_field3, {}, @values3 ); ## for each id for my $id ( keys %{ $hashref1 } ) { ## extract an array of results for that key from each hash my @results1 = @{ $hashref1->{ $id } }; my @results2 = @{ $hashref2->{ $id } }; my @results3 = @{ $hashref3->{ $id } }; ## join them together with spaces ## and print them along with the key printf "%14s : %s \t%s\t%s\n", $id, join( ' ', @results1 ), join( ' ', @results2 ), join( ' ', @results3 ); }