How do I print a hash structure in Perl?
hash, perl
Solution
Do you want to print the entire hash, or specific key, value pairs? And what is your desired result? IF it's just for debugging purposes, you can do something like:
use Data::Dumper;
print Dumper %hash; # or \%hash to encapsulate it as a single hashref entity;
You can use the `each` function if you don't care about ordering:
while ( my($key, $value) = each %hash ) {
print "$key = $value\n";
}
Or the `for` / `foreach` construct if you want to sort it:
for my $key ( sort keys %hash ) {
print "$key = $hash{$key}\n";
}
Or if you want only certain values, you can use a hash slice, e.g.:
print "@hash{qw{2009 2010}}\n";
etc, etc. There is always more than one way to do it, though it helps to know what you're frying to do first :)
Problem
Examples: ``` %hash = (2010 => 21, 2009=> 9); $hash = { a => { 0 => {test => 1}, 1 => {test => 2}, 2 => {test => 3}, 3 => {test => 4}, }, }; ``` How do I print the hash?