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) ||  ...

Replies are listed 'Best First'.
•Please don't use foo && truething || falsething
by merlyn (Sage) on Jul 30, 2002 at 14:50 UTC
    my $key = $institution && ($valid_institution{lc($institution)} || "other") || "unaffiliated";
    When I'm doing code review for hire, I do not permit using testthing && truething || falsething in production code unless there's also a big comment in the margin that says literally:
    ## I'm promising that the true branch here can NEVER EVER EVER ## return a false value, which would have caused the ## false branch to be erroneously executed. If you're ## updating this code, please continue to maintain ## this precondition, which I have placed precariously ## and needlessly in this code. Have a nice day.
    If you don't want to add a big friggin' comment like that, then change the very dangerous broken and-or construct to the entirely safe testthing ? truething : falsething, as in:
    my $key = $institution ? ($valid_institution{lc($institution)} || "other") : "unaffiliated";

    -- Randal L. Schwartz, Perl hacker

Re: Re: an easier way with grep, map, and/or sort?
by aufrank (Pilgrim) on Jul 19, 2002 at 18:00 UTC
    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.

    I couldn't agree more-- that's pretty much what motivated this question!

    It looks like it escaped from some long forgotten COBOL code if you ask me.

    never had the pleasure of even seeing COBOL code, but as I was trying to write perl, I would far prefer it look like idiomatic perl than anything else ;)

    Thanks for your response. I'll try it out just as soon as I get my head all the way around how it works :D

    --au