A hash only contains keys and values, so I'm not sure what sort of header you'd want. Perhaps printf can be of use:
printf "%20s %s\n", "Key", "Value";
printf "%20s-%s\n", "-"x20, "-----";
foreach my $key (keys %hash) {
printf "%20s %s\n", $key, $hash{$key};
}
If you're talking about a more complex data structure (such as a homogenous hash of hashes), perhaps something more like this would be to your liking:
# $hash{$key} = { field1 => 'value', ... };
my @keys = keys %{$hash{(keys %hash)[0]}};
my $format = "%10s " . ("%10s " x @keys) . "\n";
printf $format, "Key", @keys;
foreach my $key (keys %hash) {
printf $format, $key, @{$hash{$key}}{@keys};
}
|