A. Farber wrote:
> Hi, I have a simple problem, which I have solved, but
> my solution feels ugly to me. Any improvements please?
>
> A web form contains a list of project names. And for
> some projects there are mailing lists, which I need
> to display if the matching project has been selected.
>
> Some project names begin with the same string, like
> "p1881_aq_" and "p1881". In those cases only the longest
> match should be taken. Here is how I tried to solve it:
>
> # put the longest keys first, to make them win
>
> my @KEYS = qw( p1881_aq_ p1881_ p1972_ p1981_ p2311_ );
>
> my %MAIL = (p1881_aq_ => {name => 'Project Aqua',
> mail => ''},
> p1881_ => {name => 'Project Alpha',
> mail => ''},
> p1972_ => {name => 'Project Beta',
> mail => ''},
> p1981_ => {name => 'Project Gamma',
> mail => ''},
> p2311_ => {name => 'Project Theta',
> mail => ''},
> );
>
> .....
> for my $p (@selected_projects) {
> for my $k (@KEYS) {
> if (0 == index $p, $k) {
> printf q{<A HREF="MAILTO:%s">%s</A>},
> $MAIL{$k}->{mail}, $MAIL{$k}->{name};
> last;
> }
> }
> }
>
Your solution looks pretty good to me. A couple of debatable things
that might make it more "Perlish": One could make the search for keys
into a regexp, and one could interpolate the variables into the print
string rather than using printf:
$KEYS=qr/^(p1881_aq_|p1881_|p1972_|p1981_|p2311_);
...
for my $p (@selected_projects){
if($p=~$KEYS){
print "<A HREF=\"MAILTO:$MAIL{$1}->{mail}\">$MAIL{$1}->{name}</A>";
}
}
--
Bob Walton
Email:
http://bwalton.com/cgi-bin/emailbob.pl