Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Array of Hashes

by SuzuBell (Acolyte)
on Mar 21, 2013 at 04:00 UTC ( [id://1024656]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Perl Monks:

I want to print an array of 20 hashes. Each hash has 2 types of keys.

I am not too particular about how to print. I am just doing this to check that the array has all 20 hashes working, where the correct value is associated with the correct two keys. One way I might like to check the array (if possible) is to print the values from each hash of the array (That is, check that hash1 has the correct values with the correct key1s and key2s, then check that hash2 has the correct values with the correct key1s and key2s, ....).

This is the general idea of my code. The values of all hashes are all strings or empty strings.

sub getArrayHash{ $hash1s->{$key1}->{$key2} = $hash1; $hash2s->{$key1}->{$key2} = $hash2; $hash3s->{$key1}->{$key2} = $hash3; . . . $hash20s->{$key1}->{$key2} = $hash20; push( @hash_array, $hash1s, $hash2s, $hash3s, ..., $hash20s); return \@hash_array; }

So, any ideas on how to check that my hash_array has the correct values? Thank you in advance!

Replies are listed 'Best First'.
Re: Array of Hashes
by Gangabass (Vicar) on Mar 21, 2013 at 04:07 UTC
    I don't understand what you need... But try this:
    use Data::Dumper; print Dumper($hash_array);
Re: Array of Hashes
by NetWallah (Canon) on Mar 21, 2013 at 04:19 UTC
    What you have presented is an array of hash REFERENCES, in what looks like a very convoluted and inefficient structure.

    If you are having trouble expressing an algorithm or identifying a reasonable data structure, and are able to describe the original problem, perhaps we can be of assistance. Your question as posed seems too abstract (diverged from reality) and twisted to provide a coherent answer.

    Alternatively, post some (minimal) functioning code with data, and include the results of running that, and compare that with your expected results.

    P.S. Welcome to the monastery!.

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

Re: Array of Hashes
by rjt (Curate) on Mar 21, 2013 at 07:50 UTC

    Indeed, Data::Dumper will do. Data::Dump sometimes prints more compact results. Both output formats can be customized to suit your taste, but since this appears to be just debug print you need, the above will probably fit the bill.

    You can always roll your own, too, although I'd suggest you think twice or more before you go to a lot of work.

    #!/usr/bin/env perl use 5.014; use warnings; use Data::Dump; use Data::Dumper; my @hash_array = map { gen_hash() } 1..10; dd @hash_array; # Dumper(\@hash_array); # Generate a hash ref with some junk in it, two levels deep sub gen_hash { my $h; $h->{int rand 1000}->{int rand 1000} = int rand 1000 for 1..10; return $h; } __END__ Output: ( { 148 => { 570 => 799 }, 279 => { 679 => 288 }, 289 => { 2 => 464 }, 524 => { 501 => 746 }, 527 => { 122 => 839 }, 574 => { 924 => 173 }, 607 => { 295 => 240 }, 662 => { 163 => 277 }, 664 => { 157 => 606 }, 686 => { 32 => 425 }, }, { 232 => { 321 => 314 }, 358 => { 555 => 596 }, 365 => { 116 => 170 }, 579 => { 536 => 659 }, 693 => { 540 => 254 }, . . .
Re: Array of Hashes
by kcott (Archbishop) on Mar 21, 2013 at 06:14 UTC

    G'day SuzuBell,

    Welcome to the monastery.

    "I am not too particular about how to print. I am just doing this to check that the array has all 20 hashes working, where the correct value is associated with the correct two keys."

    From that description, it sounds like Data::Dumper will do what you want.

    Update: Not sure how I missed it but ++Gangabass had already suggested Data::Dumper.

    -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1024656]
Approved by kcott
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: (2)
As of 2024-04-25 03:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found