in reply to an easier way with grep, map, and/or sort?
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:
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:foreach (keys %insitution_table) { $institution_table{$_}{ages}[$age/5]++; }
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:
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.my %valid_institutions = map { $_ => $_ } ( $hospital1, $hospital2, ); # ... my $key = $institution && ($valid_institution{lc($institution)} || "other") || "unaffiliated"; $institution_table{$key}{count}++;
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:my %foo = map { $_ => 1 } qw[ foo bar baz ]; my $foo = 'foo'; print "\$foo is in the list\n" if ($foo{$foo});
... $valid_institution{lc($institution)} || ...Instead of:
... $valid_institution{lc($institution)} && lc($institution) || ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
•Please don't use foo && truething || falsething
by merlyn (Sage) on Jul 30, 2002 at 14:50 UTC | |
|
Re: Re: an easier way with grep, map, and/or sort?
by aufrank (Pilgrim) on Jul 19, 2002 at 18:00 UTC |