in reply to how to include an array of hashes in a hash of hashes?
use strict; use Data::Dumper; my %HoH = (); my $component; while ( my $line = <DATA> ) { chomp $line; next unless $line =~ m/^\S+/; if ( $line =~ m/^component=(.*)/ ) { $component = $1; next; } next unless $component; if ( $line =~ m/^version=(.*)/ ) { $HoH{$component}{"version"} = $1; next; } if ( $line =~ m/^sourcefile/ ) { my ($k, $key) = split(/=/,$line); # READ THE NEXT LINE $line = <DATA>; chomp $line; if ( $line =~ m/^sourcesum/ ) { my ($v, $value) = split(/=/,$line); $HoH{$component}{'sources'}{$key} = $value; } } } for my $component ( keys %HoH ) { my $hash = $HoH{$component}; my $version = $hash->{'version'} || ''; my $sources = $hash->{'sources'} || {}; print "$component: \n"; print "version = $version\n"; for my $key ( keys %{ $sources } ) { print "$key = $sources->{$key}\n"; } print "\n"; } print Dumper(\%HoH);
Note: this method does not keep the order of the sources. If you need to keep the order then save them as an array of arrays like so:HF: version = NULL filename1 = checksum1 filename3 = checksum3 filename2 = checksum2 SVM: version = 10.0.70.102 filename4 = checksum4 'HF' => { 'version' => 'NULL' 'sources' => { 'filename1' => 'checksum1', 'filename3' => 'checksum3', 'filename2' => 'checksum2' }, }, 'SVM' => { 'version' => '10.0.70.102' 'sources' => { 'filename4' => 'checksum4' }, }
REPLACE $HoH{$component}{'sources'}{$key} = $value; WITH push( @{ $HoH{$component}{'sources'} }, [$key,$value] ); THEN ACCESS for my $source ( @{ $sources } ) { print "$source->[0] = $source->[1]\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to include an array of hashes in a hash of hashes?
by anadem (Scribe) on Sep 19, 2013 at 00:03 UTC |