Perhaps the definitive reference for this sort of thing is in the standard Perl docs: perldsc (Perl Data Structures Cookbook). After you read and understand that, you should be on your way. For some light relief, some sample code to crudely dump ... then "pretty print" your hash of hashes:

use strict; use warnings; use Data::Dumper; my $weapons_ref = { dagger => { cost => 8, damage => 4, armor => 0 }, shortsword => { cost => 10, damage => 5, armor => 1 }, warhammer => { cost => 25, damage => 6, armor => 2 }, longsword => { cost => 40, damage => 7, armor => 0 }, greataxe => { cost => 74, damage => 8, armor => 0 }, }; print Dumper($weapons_ref); # Print by hand so it looks a bit prettier. :) for my $weapon ( sort keys %{$weapons_ref} ) { printf "%-14.14s\n", $weapon . ( '-' x 20 ); for my $attr ( sort keys %{ $weapons_ref->{$weapon} } ) { printf " %-8.8s:%3d\n", $attr, $weapons_ref->{$weapon}{$attr}; } }

which produces:

$VAR1 = { 'dagger' => { 'cost' => 8, 'armor' => 0, 'damage' => 4 }, 'greataxe' => { 'damage' => 8, 'armor' => 0, 'cost' => 74 }, 'warhammer' => { 'armor' => 2, 'damage' => 6, 'cost' => 25 }, 'shortsword' => { 'damage' => 5, 'armor' => 1, 'cost' => 10 }, 'longsword' => { 'cost' => 40, 'damage' => 7, 'armor' => 0 } }; dagger-------- armor : 0 cost : 8 damage : 4 greataxe------ armor : 0 cost : 74 damage : 8 longsword----- armor : 0 cost : 40 damage : 7 shortsword---- armor : 1 cost : 10 damage : 5 warhammer----- armor : 2 cost : 25 damage : 6

I leave summing by cost and other attributes as an exercise for the reader ... just in case it's a homework question. :)


In reply to Re: using ref to hash of hash effectively by eyepopslikeamosquito
in thread using ref to hash of hash effectively by kalee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.