Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > if ( $a eq 1 || $b eq 2...)

Reply
Thread Tools

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

 
 
Ken Sington
Guest
Posts: n/a
 
      08-05-2004
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.
 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      08-05-2004

"Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
news:vJmdnbh-yuSIGIzcRVn-...
> 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


 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      08-05-2004

"Matt Garrish" <> wrote in message
news:tvgQc.28984$ ...
>
> "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
> news:vJmdnbh-yuSIGIzcRVn-...
> > 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


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      08-05-2004
Matt Garrish <> wrote in comp.lang.perl.misc:
>
> "Ken Sington" <ken_sington@nospam_abcdefg.com> wrote in message
> news:vJmdnbh-yuSIGIzcRVn-...
> > 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
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      08-05-2004
> 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.

 
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




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57