http://www.perlmonks.org?node_id=808302
mrras25 has asked for the wisdom of the Perl Monks concerning the following question:

I need help putting the output of a command into a hash of hashes. The problem is that each "Name" in the output is different but contained in that name out their could be multiple "keys" for the value I am trying to capture

I want to capture the "Name" and the unique 1st key identifier, and then each of the following key and value pair (CloneCount, CloneCount,CloneState,IsDirty, PercentSynced) As you can see from below some names have multiple cloneID and values associated with that cloneID

Here is the output its just a sample of the output:

Name: LUN 120 DB CloneGroupUid: 50:06:01:60:BC:E0:26:09:03:00:00:00:00:00:00:00 InSync: Yes Description: QuiesceThreshold: 60 SourceMediaFailure: No IsControllingSP: Yes SourceLUNSize: 587202560 CloneCount: 3 Sources: 120 Clones: CloneID: 0100000000000000 CloneState: Consistent CloneCondition: Administratively Fractured AvailableForIO: Yes CloneMediaFailure: No IsDirty: No PercentSynced: N/A RecoveryPolicy: Auto SyncRate: High CloneLUNs: 200 UseProtectedRestore: No IsFractured: Yes CloneID: 0300000000000000 CloneState: Consistent CloneCondition: Normal AvailableForIO: No CloneMediaFailure: No IsDirty: No PercentSynced: N/A RecoveryPolicy: Auto SyncRate: Medium CloneLUNs: 202 UseProtectedRestore: No IsFractured: No CloneID: 0200000000000000 CloneState: Consistent CloneCondition: Administratively Fractured AvailableForIO: Yes CloneMediaFailure: No IsDirty: No PercentSynced: N/A RecoveryPolicy: Auto SyncRate: High CloneLUNs: 201 UseProtectedRestore: No IsFractured: Yes Name: LUN 110 audit trail CloneGroupUid: 50:06:01:60:BC:E0:26:09:08:00:00:00:00:00:00:00 InSync: Yes Description: QuiesceThreshold: 60 SourceMediaFailure: No IsControllingSP: Yes SourceLUNSize: 629145600 CloneCount: 1 Sources: 110 Clones: CloneID: 0100000000000000 CloneState: Consistent CloneCondition: Administratively Fractured AvailableForIO: Yes CloneMediaFailure: No IsDirty: No PercentSynced: N/A RecoveryPolicy: Auto SyncRate: High CloneLUNs: 213 UseProtectedRestore: No IsFractured: Yes

Replies are listed 'Best First'.
Re: Command output into a hash of hash
by colwellj (Monk) on Nov 20, 2009 at 00:31 UTC
    it seems to me you want a hash of hashes of arrays.
    so something akin to this?
    my %name; my $cur_name; while(<YOUR_FILE>){ chomp; my ($key,$value) = split(/:\s{2}/,$_); if ($key !~ /^$/){ if ($key =~ /^Name/){ $cur_name = $value }else{ push @{$name{$cur_name}{$key}},$value; } } }
      Thank you kindly - This will do!!!!
Re: Command output into a hash of hash
by toolic (Bishop) on Nov 20, 2009 at 00:27 UTC
    'LUN 120 DB' => [ {CloneID=>0100000000000000, CloneState=>Consistent, etc...}, {CloneID=>0300000000000000, CloneState=>Consistent, etc...}, ]

      yes I know how to create a hash of hashes I need to parse the command returned output and place that into a hash of hashes

      while(<CMD>) { ... build my hash ... }
Re: Command output into a hash of hash
by bichonfrise74 (Vicar) on Nov 20, 2009 at 01:05 UTC
    Another way...
    #!/usr/bin/perl use strict; use Data::Dumper; my %record; my $name; while (<DATA>) { if ( /Name: / ... /\n\n/ ) { s/Name:\s*(.*)// and $name = $1; my ($key, $val) = split( /\s+/ ); push( @{ $record{$name}->{$key} }, $val ); } } print Dumper \%record; __DATA__ Name: LUN 120 DB CloneGroupUid: 50:06:01:60:BC:E0:26:09:03:00:00:00:00:00:00:00 InSync: Yes ...