Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Printing hash of a hash data

by eanicom (Initiate)
on Sep 17, 2012 at 21:04 UTC ( [id://994104]=perlquestion: print w/replies, xml ) Need Help??

eanicom has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Compatriots,

I'm interested in knowing how to display the data in a hash of hashes using printf. I'm familiar with how to iterate through a typical hash and display the data using printf to format it but I'm having a difficult time displaying the data for a hash of hashes to format it with printf to get it to display how i want it. I know it must be easy but I'm having a bit of a difficult time. Any help would be greatly appreciated

For example,

if the Data is organized as such in the hash of hashes:

James' => { 'detection_profile' => 'disable' }, 'Bob' => { 'detection_profile' => 'enable', 'detection_profile_severity' => '180' }, 'Billy' => { 'detection_profile' => 'enable', 'detection_profile_severity' => '180' }, 'Stephanie' => { 'detection_profile' => 'enable', 'detection_profile_severity' => '180' },

How can I iterate through the hash of hashes to display the data as such:

James disable Bob enable 180 Billy enable 180 Stephanie enable 180

Replies are listed 'Best First'.
Re: Printing hash of a hash data
by davido (Cardinal) on Sep 17, 2012 at 21:32 UTC

    my %hash = ( # the hash you demonstrated ); foreach my $name ( keys %hash ) { print "$key $hash{$key}{detection_profile}", exists $hash{$key}{detection_profile_severity} ? " $hash{$key}{detection_profile_severity}\n" : "\n"; }

    If for some reason you wish to use printf instead, the POD should be all you need to make that minor change.


    Dave

Re: Printing hash of a hash data
by kennethk (Abbot) on Sep 17, 2012 at 21:10 UTC
    What have you tried? What didn't work? See How do I post a question effectively?. There's plenty of data on producing formatting strings in sprintf. Aside from that, simply iterating over the hash using Foreach Loops acting on keys should get you your loop. Then dereference each hashref as per perlreftut (e.g. print $hash{Bob}->{'detection_profile'};).

    I also note that the enable/disable switch changes your output formatting, which implies to me that you don't really want printf, but maybe I'm missing something because you didn't wrap your input/output in <code> tags.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Printing hash of a hash data
by 2teez (Vicar) on Sep 17, 2012 at 21:57 UTC
    Hi,

    You can easily get that done like so:

    #!/usr/bin/perl use warnings; use strict; my %my_hash = ( 'James' => { 'detection_profile' => 'disable' }, 'Bob' => { 'detection_profile' => 'enable', 'detection_profile_severity' => '180' }, 'Billy' => { 'detection_profile' => 'enable', 'detection_profile_severity' => '180' }, 'Stephanie' => { 'detection_profile' => 'enable', 'detection_profile_severity' => '180' }, ); foreach my $detective ( keys %my_hash ) { print $detective; while ( my ( $key, $value ) = each $my_hash{$detective} ) { print " ", $value; } print $/; }

    output
    Stephanie enable 180 James disable Bob enable 180 Billy enable 180
    please, you might also like to check perldsc

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      hello i attempted to execute your snippet in order to explore but i got the following error:

      Type of arg 1 to each must be hash (not hash element) at ./test.pl line 24, near "} ) " Execution of ./test.pl aborted due to compilation errors.

        Try:
        while ( my ( $key, $value ) = each %{ $my_hash{$detective} }) {

                     I hope life isn't a big joke, because I don't get it.
                           -SNL

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://994104]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found