Longest match wins - how to do it Perl way?

Discussion in 'Perl Misc' started by A. Farber, May 1, 2004.

  1. A. Farber

    A. Farber Guest

    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;
    }
    }
    }
    A. Farber, May 1, 2004
    #1
    1. Advertising

  2. A. Farber

    Bob Walton Guest

    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
    Bob Walton, May 2, 2004
    #2
    1. Advertising

  3. A. Farber

    John Bokma Guest

    John Bokma, May 2, 2004
    #3
  4. A. Farber

    Big and Blue Guest

    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. -*-
    Big and Blue, May 3, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joerg Schuster

    leftmost longest match (of disjunctions)

    Joerg Schuster, Dec 1, 2003, in forum: Python
    Replies:
    12
    Views:
    680
    Joerg Schuster
    Dec 3, 2003
  2. Licheng Fang
    Replies:
    32
    Views:
    1,232
    Tim Peters
    Sep 17, 2006
  3. John Gordon
    Replies:
    13
    Views:
    458
    Ian Kelly
    Dec 20, 2011
  4. longest prefix match

    , Nov 24, 2008, in forum: Perl Misc
    Replies:
    5
    Views:
    545
    Ted Zlatanov
    Nov 24, 2008
  5. Replies:
    11
    Views:
    795
    Peter J. Holzer
    Nov 29, 2008
Loading...

Share This Page