Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2021-06-20 06:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (94 votes). Check out past polls.

    Notices?