if ( $a eq 1 || $b eq 2...)

Discussion in 'Perl Misc' started by Ken Sington, Aug 5, 2004.

  1. Ken Sington

    Ken Sington Guest

    this is starting to bother me:

    if (
    $req =~ /\.html$/ ||
    $req =~ /\.pl$/ ||
    $req =~ /\.shtml$/ ||
    $req =~ /\.txt$/ ||
    $req =~ /\.cgi$/ ||
    $req =~ /\.cgi\?/ ||
    $req =~ /\/\?/ ||
    $req =~ /\/$/
    ) {
    $countPage++;
    }


    there's got to be a better way.
     
    Ken Sington, Aug 5, 2004
    #1
    1. Advertising

  2. Ken Sington

    Matt Garrish Guest

    "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
    news:...
    > this is starting to bother me:
    >
    > if (
    > $req =~ /\.html$/ ||
    > $req =~ /\.pl$/ ||
    > $req =~ /\.shtml$/ ||
    > $req =~ /\.txt$/ ||
    > $req =~ /\.cgi$/ ||
    > $req =~ /\.cgi\?/ ||
    > $req =~ /\/\?/ ||
    > $req =~ /\/$/
    > ) {
    > $countPage++;
    > }
    >
    >
    > there's got to be a better way.


    I see three different clusters that just can't be merged. The following will
    decrease the readability immensely, but will compact the code if that's all
    you want:

    if ( $req =~ m#(\.(s?html?|pl|cgi|txt)$)|(/[$?])|(\.(pl|cgi)\?)# ) {
    $countPage++; }

    Matt
     
    Matt Garrish, Aug 5, 2004
    #2
    1. Advertising

  3. Ken Sington

    Matt Garrish Guest

    "Matt Garrish" <> wrote in message
    news:tvgQc.28984$...
    >
    > "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
    > news:...
    > > this is starting to bother me:
    > >
    > > if (
    > > $req =~ /\.html$/ ||
    > > $req =~ /\.pl$/ ||
    > > $req =~ /\.shtml$/ ||
    > > $req =~ /\.txt$/ ||
    > > $req =~ /\.cgi$/ ||
    > > $req =~ /\.cgi\?/ ||
    > > $req =~ /\/\?/ ||
    > > $req =~ /\/$/
    > > ) {
    > > $countPage++;
    > > }
    > >
    > >
    > > there's got to be a better way.

    >
    > I see three different clusters that just can't be merged. The following

    will
    > decrease the readability immensely, but will compact the code if that's

    all
    > you want:
    >
    > if ( $req =~ m#(\.(s?html?|pl|cgi|txt)$)|(/[$?])|(\.(pl|cgi)\?)# ) {
    > $countPage++; }
    >


    Oops! For some reason I thought the $ in the last expression was escaped (I
    also should have escaped it in the character class (/[\$?]), so double my
    bad!):

    if ( $req =~ m#(\.(s?html?|pl|cgi|txt)$)|(/\?)|(\.(pl|cgi)\?)# ) {
    $countPage++; }

    Matt
     
    Matt Garrish, Aug 5, 2004
    #3
  4. Ken Sington

    Anno Siegel Guest

    Matt Garrish <> wrote in comp.lang.perl.misc:
    >
    > "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
    > news:...
    > > this is starting to bother me:
    > >
    > > if (
    > > $req =~ /\.html$/ ||
    > > $req =~ /\.pl$/ ||
    > > $req =~ /\.shtml$/ ||
    > > $req =~ /\.txt$/ ||
    > > $req =~ /\.cgi$/ ||
    > > $req =~ /\.cgi\?/ ||
    > > $req =~ /\/\?/ ||
    > > $req =~ /\/$/
    > > ) {
    > > $countPage++;
    > > }
    > >
    > >
    > > there's got to be a better way.

    >
    > I see three different clusters that just can't be merged. The following will
    > decrease the readability immensely, but will compact the code if that's all
    > you want:
    >
    > if ( $req =~ m#(\.(s?html?|pl|cgi|txt)$)|(/[$?])|(\.(pl|cgi)\?)# ) {
    > $countPage++; }


    How about a compromise (untested):

    $countPage ++ if
    $req =~ /\.(?:html|pl|shtml|txt|cgi)$/ ||
    $req =~ /\.cgi\?/ ||
    $req =~ /\/\?/ ||
    $req =~ /\/$/;

    or even

    /\.(?:html|pl|shtml|txt|cgi)$/ || /\.cgi\?/ || /\/\?/ || /\/$/ and
    $countPage ++ for $req;

    Anno
     
    Anno Siegel, Aug 5, 2004
    #4
  5. > if (
    > $req =~ /\.html$/ ||
    > $req =~ /\.pl$/ ||
    > $req =~ /\.shtml$/ ||
    > $req =~ /\.txt$/ ||
    > $req =~ /\.cgi$/ ||
    > $req =~ /\.cgi\?/ ||
    > $req =~ /\/\?/ ||
    > $req =~ /\/$/
    > ) {
    > $countPage++;
    > }


    In addition to other ways you've been shown you can combine all the
    simple suffix ones

    $req =~ /\.(\w+)$/ && $suffix{$1}

    But this is probably not worth it for only five suffixes.
     
    Brian McCauley, Aug 5, 2004
    #5
    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.

Share This Page