in reply to hashes with multiple keys
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $mode; my %vol; my %host; while(<DATA>){ next if /^\s*$/; next if /^volume_name/; if (/^host_output/){ $mode = "host"; next; } if (/^volume_output/){ $mode = "volume"; next; } my ($k,$v) = split /\s+/; if ($mode eq 'host'){ $vol{$k}{host} = $v; } elsif ($mode eq 'volume') { $vol{$k}{size} = $v; } } for my $k ( keys %vol ) { $host{ $vol{$k}{host} }{$k} = $vol{$k}{size}; } for my $k ( keys %host ) { no warnings; $host{$k}{total_size} += $_ for values %{$host{$k}}; } print Dumper (\%host); __DATA__ host_output volume_name host_name vol1 host1 vol2 host1 vol3 host1 vol4 host2 vol5 host2 vol2 host2 volume_output volume_name size vol1 10g vol2 20g vol3 30g vol4 30g vol5 20g __END__ $VAR1 = { 'host2' => { 'vol2' => '20g', 'vol4' => '30g', 'vol5' => '20g', 'total_size' => '70' }, 'host1' => { 'vol1' => '10g', 'total_size' => '40', 'vol3' => '30g' } };
print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});
|
|---|