Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Longest match wins - how to do it Perl way?

Reply
Thread Tools

Longest match wins - how to do it Perl way?

 
 
A. Farber
Guest
Posts: n/a
 
      05-01-2004
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 => '(E-Mail Removed)'},
p1881_ => {name => 'Project Alpha',
mail => '(E-Mail Removed)'},
p1972_ => {name => 'Project Beta',
mail => '(E-Mail Removed)'},
p1981_ => {name => 'Project Gamma',
mail => '(E-Mail Removed)'},
p2311_ => {name => 'Project Theta',
mail => '(E-Mail Removed)'},
);

......
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;
}
}
}
 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      05-02-2004
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 => '(E-Mail Removed)'},
> p1881_ => {name => 'Project Alpha',
> mail => '(E-Mail Removed)'},
> p1972_ => {name => 'Project Beta',
> mail => '(E-Mail Removed)'},
> p1981_ => {name => 'Project Gamma',
> mail => '(E-Mail Removed)'},
> p2311_ => {name => 'Project Theta',
> mail => '(E-Mail Removed)'},
> );
>
> .....
> 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

 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      05-02-2004
Bob Walton wrote:

> print "<A HREF=\"MAILTO:$MAIL{$1}->{mail}\">$MAIL{$1}->{name}</A>";


using qq() you don't need to escape the " inside, or you can use single
quotes for the href value.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced Perl programmer available: http://castleamber.com/
 
Reply With Quote
 
Big and Blue
Guest
Posts: n/a
 
      05-03-2004
A. Farber wrote:
>
> # 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',


A bit of a maintenance problem? Why not let Perl generate @KEYS?

my @KEYS = sort { length($b) <=> length($a) } keys %MAIL;

> for my $p (@selected_projects) {
> for my $k (@KEYS) {


last if (length($k) < length(p)); # Useful if you have a lot

> if (0 == index $p, $k) {


--
-*- Just because I've written it here doesn't -*-
-*- mean that you should, or I do, believe it. -*-

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
re.sub(): replace longest match instead of leftmost match? John Gordon Python 13 12-20-2011 02:58 AM
Longest Sites Name With Worlds Longest Stuffs... Vijay Murthy Computer Information 0 02-04-2006 09:57 AM
WINS server not a WINS client Corrado Labinaz MCSE 2 03-07-2004 06:05 PM
leftmost longest match (of disjunctions) Joerg Schuster Python 12 12-03-2003 12:37 PM



Advertisments