Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Array of Hashes

by SuzuBell (Acolyte)
on Mar 21, 2013 at 04:00 UTC ( #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 (Abbot) 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 (Deacon) 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 (Canon) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1024656]
Approved by kcott
[Corion]: Once more, I'm looking for a sane client-side framework, but I guess these don't exist. Everything I look at either uses a weirdo home-grown templating language (like Angular in all its incarnations) or uses weirdo Javascript incarnations (like ...
[Corion]: ... Inferno.js, which uses ES2015) or uses some horrible amount of Javascript infrastructure before you can even render a single file.
[Corion]: I'd really like to create a dynamic frontend for my Google Keep clone, but so far, all the templating solutions seem to bring their own template language or require me to hand-code everything in (their own flavour of) Javascript. I'd like something ...
[Corion]: ... that can use templates from the HTML (via <script language="text/ template">... my template ...</script> and otherwise gets out of my way. But it seems that is not how the Javascript world is, currently :-/
[Corion]: I guess my question is, has anybody worked with React, Ember, Angular (v2, v4, v.whatever) or any other JS client-side dynamic rendering/virtual DOM thingie and found a simple, single-file solution like jQuery?

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2017-03-27 19:03 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (321 votes). Check out past polls.