Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

array of hashes?

by Mark.Allan (Sexton)
on Oct 27, 2013 at 12:49 UTC ( #1059898=perlquestion: print w/ replies, xml ) Need Help??
Mark.Allan has asked for the wisdom of the Perl Monks concerning the following question:

Monks

I've come up against a problem which I think can be resolved with an array of hashes but im not quite sure how to proceed i've tried various ways but its starting to frustrate a little now.

I have an hash of hashes data structure which is work fine for one key to one value

$VAR1 = 'mode'; $VAR2 = { 'type' => ' auto mode', 'action' => ' completed on own.' };

An using the below snippet of code to print the hash values out

for(sort keys %component) { print "$_,$component{$_}{type},$component{$_}{action}\n"; }

The problem is, some times the key can have different values based on an override switch

$VAR1 = 'mode' $VAR2 = { 'switch1' => { 'type' => ' auto mode for switch1', 'action' => ' completed with assistance.' }, 'switch2' => { 'type' => ' auto mode for switch2', 'action' => 'completed on own.' }, 'switch3' => { 'type' => ' auto mode for switch3' 'action' => ' rasied alarm, incident reporte +d.', }, 'switch4' => { 'type' => ' manual mode for switch4', 'action' => ' complete process manually

Any assistance on how I could generate and print this data structure would be greatly appreciated

Comment on array of hashes?
Select or Download Code
Re: array of hashes?
by toolic (Chancellor) on Oct 27, 2013 at 13:08 UTC
Re: array of hashes?
by jethro (Monsignor) on Oct 27, 2013 at 14:07 UTC

    For your simple hash you already used one loop. For a deeper structure like a Hash of Hashes (or an Array of Hashes) you need two loops, one inside the other:

    for my $key1 (sort keys %component) { for my $key2 (sort key $component{$key1} ) { #do something with $key1, $key2 and $component{$key1}{$key2} ...
Re: array of hashes?
by marinersk (Chaplain) on Oct 27, 2013 at 14:31 UTC
    Definitely read perldsc as per toolic's advice.

    However, your question doesn't seem to match the title; or at least, I'm slow enough without my first cup of coffee this morning that I don't see how you're heading anywhere near an Array of Hashes (hereafter, AoH) with your problem.

    Therefore, at the end of this post, I will give you a working example of an AoH in hopes that this will bridge the gap. Absent that, the conversation will hopefully lead us in a useful direction.

    What your last paragraph and code suggest is that you are heading toward continued use of your hash as it exists, merely changing the keys based on a switch.

    The purpose of an array is to have a list (no pun intended) of items which are simply stacked up and conveniently accessed. Traditionally accessed via their index value ([0] through [n]), Perl stepped up and gave us an indexless way to work through the queue in its most common use case: Sequentially processing each element. Perl's genius addition was the foreach loop.

    So an Array of Hashes is a way to stack up a bunch of hashes in one place so they can be accessed without particular attention to how many of them there are, where they can later be processed (likely in a foreach loop).

    I am really not making the connection between your description and the need to track multiple hashes from a single point.

    Can you clarify? Or is it possible that AoH isn't exactly what you were looking for?

    In the meantime, here's a sample use of AoH:

    C:\Steve\Dev\PerlMonks\P-2013-10-27@0815-Array-of-Hashes>perl testAoH. +pl -----[ New Hash from Array ]--------------- Key {ABC} contains '2' Key {DEF} contains '3' -----[ New Hash from Array ]--------------- Key {GHI} contains '5' Key {JKL} contains '6' -----[ New Hash from Array ]--------------- Key {MNO} contains '8' Key {PQR} contains '9' -------------------------------------------
Re: array of hashes?
by AnomalousMonk (Abbot) on Oct 27, 2013 at 15:16 UTC
    ... assistance on how I could ... print this data structure ...

    A way to better print the data structure during development and debug (and when communicating with your fellow monks) is to properly use structure dumping tools like Data::Dumper and Data::Dump. For instance, you show one dump as

    $VAR1 = 'mode'; $VAR2 = { 'type' => ' auto mode', 'action' => ' completed on own.' };
    which suggests a statement like
        print Dumper %hash;
    was used to generate it. This use of Dumper and its siblings and cousins such as dd in Data::Dump flattens out the top-level key/value pairs of the hash into a representation that quickly becomes incomprehensible, IMHO, for large structures (although I think dd does a bit better with it than Dumper).

    Properly passing the structure by reference leads to a much more comprehensible representation. Ferinstance:

Re: array of hashes?
by 2teez (Priest) on Oct 27, 2013 at 19:50 UTC

    Hi Mark.Allan,

    Any assistance on how I could generate..

    The data structure you have is not array of hashes as your title suggested and as it has been rightly pointed out. Your data structure as presented is HASHES OF HASHES, atleast as far as I can see.

    ..and print this data structure..
    If I may give you a head up, something like this can do for your OP dataset, of course, please give heed to the advises of the monks who had commented before now.

    use warnings; use strict; use Data::Dumper; my %com = ( 'mode' => { 'switch1' => { 'type' => ' auto mode for switch1', 'action' => ' completed with assistance.' }, 'switch2' => { 'type' => ' auto mode for switch2', 'action' => 'completed on own.' }, 'switch3' => { 'type' => ' auto mode for switch3', 'action' => ' rasied alarm, incident reported.' }, 'switch4' => { 'type' => ' manual mode for switch4', 'action' => ' complete process manually', }, }, ); # this shows what you have originally # with sorted keys though { local $Data::Dumper::Sortkeys = 1; print Dumper %com; } # you can print out on your own foreach my $key ( sort keys %com ) { print $key, $/; check_and_print( $com{$key} ); } sub check_and_print { my $val = shift; if ( ref $val eq 'HASH' ) { for my $key ( sort keys %{$val} ) { print q{ }, $key, $/; check_and_print( $val->{$key} ); } } else { print q{ }, $val, $/; } }

    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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-10-21 04:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (95 votes), past polls