I feel like I should go to confession to say this, but here goes: I'm not very good with map, and terrible with sort and grep. I am, however, getting to the point that I can tell when I should be using one of these functions, and I think I've found just such a case. Sadly, I don't know what to do now, as I certainly don't want to go on write more unnecessary and inefficient lines of code, but don't have the skills I need to do it in better way.

I need to go through an array of hashes where the hashes all have the keys instiution, race, age. I need to know how many men in various age ranges are enrolled in each institution, and how many men of those age ranges are of each race. I started to do something like this:

# there are actually lots more institutions than this, # but this gets the point across if (!defined $institution) { $institution_table{"unaffiliated"} ++; } elsif ($institution =~ /^$hospital1$/i) { $institution_table{$hospital1} ++; } elsif ($institution =~ /^$hospital2$/i) { $institution_table{$hospital2} ++; } else { $institution_table{"other"} ++; } foreach (sort keys %institution_table) { $institution_table{$_}{"fortyfive_fifty"} ++ if (45 < $age <= 50); $institution_table{$_}{"fifty_fiftyfive"} ++ if (50 < $age <= 55); $institution_table{$_}{"fiftyfive_sixty"} ++ if (55 < $age <= 60); #...and so on up through the 85-90 age bracket }
aside from being really ugly, I'm pretty sure it's not even close to being the best way to do this. I've got a strong intuition that the answers lie in doing sexy things with grep, map, and/or sort, but that's about all I've got (aside from a pile of ugly code).

So, any suggestions on how to find the number of men in each age range at each hospital?

thanks for any help,
--au


In reply to an easier way with grep, map, and/or sort? by aufrank

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.