%hash = ( one => [1,2,3,4,5], two => [a,b,c,d,e], the => [l,m,n,o,p] ); foreach my $key ( keys %hash ) { push @cols, $key; push @vals, scalar( join ",", @{$hash{$key}} ); } foreach $val ( @vals ) { print "$val"; print "\n"; } print "\n"; $statement = 'insert into this (' . (join ",", @cols) . ') values (' . (join ",", @vals) . ')'; print "$statement\n\n"; #### /home/jptxs > perl sortHashForInsert l,p a,b,c,d,e 1,2,3,4,5 #### /home/jptxs > perl sortHashForInsert 9,8,7,6,5 a,b,c,d,e 1,2,3,4,5