http://qs321.pair.com?node_id=183334


in reply to an easier way with grep, map, and/or sort?

For one, you can't use chained comparisons until Perl 6. Instead, you have to do this:
if (55 < $age && $age <= 60) ...
When dealing with ranges, if they are predictable intervals, you can do math and end up with something like this:
foreach (keys %insitution_table) { $institution_table{$_}{ages}[$age/5]++; }
This builds an array of 5-year grouped ages. Consider how difficult it would be to iterate through a listing which uses text names for ranges. "fifty" comes before "forty" in the alphabet, and "twenty" comes after. If you need to know who's in the 50-55 age group:
my $sample = $insitution_table{$_}{ages}[60/5];
As for your initial block, you're using regular expressions when you could be using something much simpler:
my %valid_institutions = map { $_ => $_ } ( $hospital1, $hospital2, ); # ... my $key = $institution && ($valid_institution{lc($institution)} || "other") || "unaffiliated"; $institution_table{$key}{count}++;
This sets $key to be the appropriate spot to insert. You can add new types to the list and no new code is required. If there's one thing that really irritating, it's these endless chained "if" statements that do very little but take up a ton of room. Sure, if you get paid per line of code, you might have a case, but still. It looks like it escaped from some long forgotten COBOL code if you ask me.

Also, I've put 'count' at the end because you were using the hash in an invalid way, something you would notice with use strict; Note that you were putting a number into, for example, $institution_table{$foo} and then later using this number as a hash when you do this: $institution_table{$foo}{bar}++

Update:
By request, here's a quick explanation about what the map was doing. I've used it here to turn a list of variables into a hash, so that later, it is very easy to see if another variable is in this original list. Typically, this is done like this:
my %foo = map { $_ => 1 } qw[ foo bar baz ]; my $foo = 'foo'; print "\$foo is in the list\n" if ($foo{$foo});
However, since in this case the output value of the hash was going to be used for something else, I just assigned it to the same value. This way the code looks like this:
... $valid_institution{lc($institution)} || ...
Instead of:
... $valid_institution{lc($institution)} && lc($institution) ||  ...