in reply to Print from HoH, in defined order, skipping missing keys

You need two passes, one to build a list of sub-keys, and a second to print the table. Consider:

use strict; my %hash = ( Downtown_Market => {Fruit => 1, More_fruit => 2, Even_more_fruit = +> 3}, Uptown_market => {More_fruit => 4, Fruit => 5}, East_market => {Even_more_fruit => 6}, ); my %columnLookup; my @columns; my $maxKeyLen = 0; # Build list of sub-keys for my $key (keys %hash) { $maxKeyLen = length $key if length $key > $maxKeyLen; for my $subKey (keys %{$hash{$key}}) { next if exists $columnLookup{$subKey}; $columnLookup{$subKey} = @columns; push @columns, $subKey; } } # Print the table for my $key (sort keys %hash) { printf "%-*s (", $maxKeyLen + 2, "$key :"; print join ', ', map {exists $hash{$key}{$_} ? $_ : ' ' x length $_} @columns; print ")\n"; }

Prints:

Downtown_Market : (Even_more_fruit, More_fruit, Fruit) East_market : (Even_more_fruit, , ) Uptown_market : ( , More_fruit, Fruit)

Perl is environmentally friendly - it saves trees