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:
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).# 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 }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |