in reply to How do I print nested hashes
You've got a few problems, not the least of which is that your datastructure isn't a nested hash of hashes. It's a hash of lists of hashes, or something wierd like that. Use Data::Dumper to view the datastructure first to see how it doesn't match what you're trying to print.
You should also construct your map statement like this:
print map "$_ = $ref->{$_}\n", sort keys %{$ref};Note the $ref->{$_} construct. You have to dereference the hashref $ref.
Here's code that works. It's sloppy because you're still not using strict, not using warnings, formatting is all over the place, etc. But it does what you're after:
my %Recipes = ( "YumYumStuff" => { ingredients => { 1 => "1 Pate a Bombe", 2 => "1/2 French Meringue", 3 => "300ml double cream", 4 => "3 very ripe bananas", 5 => "2 tablespoons Creme de Banane", 6 => "1 Bitter Chocolate Sorbet", }, instructions => { 1 => "Whisk together the pate a bombe and the French Meringue +mixtures. They should be roughly equal in amounts by volume.", 2 => "Lightly whip the cream to the same consistency as the pa +te de bombe and French Meringue mixture then mix both mixtures togeth +er", 3 => "Finally, whisk in the banana puree and liquer. Pour or +pipe into ramekins, a long loaf tin or a large freezer container. ", 4 => "About 30 mins before serving, transfer the parfait from +the freezer to the refrigerator", } } ); for $recipe (keys %Recipes) { print "For $recipe the ingredients are :\n"; $ref = $Recipes{$recipe}{ingredients}; print map "$_ = $ref->{$_}\n", sort keys %{$ref}; }
Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How do I print nested hashes
by blueapache (Acolyte) on Aug 30, 2004 at 20:24 UTC |