How can I parse through this?

There are many ways you could parse your file. Here is one way:

#!/usr/bin/perl use strict; use warnings; my $section = "unknown"; my %volumes; # volumes for each host, keyed by host name my %capacities; # capacity of each volume, keyed by volume name while(my $line = <DATA>) { chomp($line); if ( $line =~ m/^host_output$/) { $section = "host_output"; my $discard = <DATA>; # discard the header line } elsif ( $line =~ m/^volume_output$/) { $section = "volume_output"; my $discard = <DATA>; # discard the header line } elsif ( $line =~ m/^$/) { $section = "unknown"; } elsif ($section eq "host_output") { my ($volume, $host) = split(/\s+/,$line); push(@{$volumes{$host}}, $volume); } elsif ( $section eq "volume_output" ) { my ($volume, $capacity) = split(/\s+/,$line); $capacities{$volume} = $capacity; } } foreach my $host (sort keys %volumes) { foreach my $volume (@{$volumes{$host}}) { print "$host: $volume: $capacities{$volume}\n"; } } __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

Which produces:

host1: vol1: 10g host1: vol2: 20g host1: vol3: 30g host2: vol4: 30g host2: vol5: 20g host2: vol2: 20g

This example makes several assumptions about your data, which you should consider carefully.

Update: removed extraneous "next;"


In reply to Re: hashes with multiple keys by ig
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.