a nested hash approach
#!/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']});

In reply to Re: hashes with multiple keys by codeacrobat
in thread hashes with multiple keys by annie06

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.