You can't use a straight hash of hashes because he seems to want the original order of office 1, bedroom 2, etc. preserved, and a hash won't do that. You also have to provide for there being empty lines.
use strict; use warnings; my (@arr, $ref); while (<DATA>) { chomp; if (/^[a-zA-Z]/) { $arr[$#arr+1][0] = $_; } elsif (/^\d/) { $arr[$#arr][1]{$_}++; } } foreach (@arr) { print @$_[0] . "\n\n"; print "width height quantity\n\n"; $ref = @$_[1]; for (sort {$ref->{$b} <=> $ref->{$a} or $a cmp $b} keys %$ref) { print "$_ " . $ref->{$_} . "\n\n"; } } __DATA__ office 1 120 120 120 120 140 135 155 135 120 120 bedroom 2 100 75 100 75 120 180

In reply to Re: Data Structures Help by TedPride
in thread Data Structures Help by Anonymous Monk

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.