Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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}++

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

In reply to Re: an easier way with grep, map, and/or sort? by tadman
in thread an easier way with grep, map, and/or sort? by aufrank

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2023-03-31 19:20 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (76 votes). Check out past polls.