key1 key2 key3 key4 key5 $data{key1}[0] $data{key2}[0] $data{key3}[0] $data{key4}[0] $data{key5}[0] $data{key1}[1] $data{key2}[1] $data{key3}[1] $data{key4}[1] $data{key5}[1] $data{key1}[2] $data{key2}[2] $data{key3}[2] $data{key4}[2] $data{key5}[2] ... #### my $col_width = 20; my @keys = sort keys %data; my $p_format = join(" " => ("%${col_width}s") x @keys) . "\n"; ## $p_format would be "%20s %20s %20s ... %20s\n", so the columns will ## line up even if the data strings are different sizes printf $p_format, @keys; my $i = 0; LOOP: { ## important part: pull out the ith entry in each array my @ith_data = map { $data{$_}[$i] || "" } @keys; ## you mentioned the data strings end in \n chomp @ith_data; printf $p_format, @ith_data; $i++; ## loop again if any array has elements left for (@keys) { redo LOOP if exists $data{$_}[$i]; } } #### my @keys = sort keys %data; for my $i ( 0 .. $size-1 ) { my @ith_data = map { $data{$_}[$i] || "" } @keys; chomp @ith_data; print "@ith_data\n"; }