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

K

Ken Sington

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.
 
M

Matt Garrish

Ken Sington said:
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
 
M

Matt Garrish

Matt Garrish said:
Ken Sington said:
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
 
A

Anno Siegel

Matt Garrish said:
Ken Sington said:
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
 
B

Brian McCauley

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top