aufrank has asked for the wisdom of the Perl Monks concerning the following question:
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:
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: an easier way with grep, map, and/or sort?
by tadman (Prior) on Jul 19, 2002 at 17:45 UTC | |
by merlyn (Sage) on Jul 30, 2002 at 14:50 UTC | |
by aufrank (Pilgrim) on Jul 19, 2002 at 18:00 UTC | |
Re: an easier way with grep, map, and/or sort?
by kvale (Monsignor) on Jul 19, 2002 at 17:55 UTC | |
Re: an easier way with grep, map, and/or sort?
by broquaint (Abbot) on Jul 19, 2002 at 17:51 UTC | |
Re: an easier way with grep, map, and/or sort?
by aufrank (Pilgrim) on Jul 19, 2002 at 17:43 UTC |
Back to
Seekers of Perl Wisdom