On Jun 7, Teresa Raymond said:
>How can we make this code more succinct?
I'll answer a different question:
"How can we make this code more readable, so that people will bother to
look twice at it?"
Don'twritePerlasifthere'snotimeleftinyourdaytodoitwell.
Youshouldtry
tohaveanice
indentationsystemthat
makessensetoyouso
thatit'seasytotellwhatgoes
where.And
rememberthatwhitespaceisyourfriend.
>#LOOP TO INITIALIZE VARIABLES + TEST N COMMANDS FOR MATCH IS YES
>foreach $i (sort(@indata))
>{chop($i);
>($aptname,$address,$city,$zip,$phone,$location,$bedrooms,$rentmin,$ren
>tmax,$pets,$laundry,$garage,$comment,$aptweb,$aptemail,$graphic)=split
>(/\|/,$i);
for (sort @indata) {
chomp; # probably safer than chop() here
my ($aptname, $address, $city, $zip, $phone,
$location, $bedrooms, $rentmin, $rentmax,
$pets, $laundry, $garage, $comment, $aptweb,
$aptemail, $graphic) = split /\|/;
> if (($in{'location'} eq "All" || $location=~/$in{'location'}/ig) &&
>($in{'bedrooms'} eq "Any" || $bedrooms=~/$in{'bedrooms'}/ig ||
>($in{'bedrooms'} eq "4 plus Bedrooms" && ($bedrooms=~/4 Bedrooms/ig
>|| $bedrooms=~/5 Bedrooms/ig || $bedrooms=~/6 Bedrooms/ig))) &&
>($rentmin<=$in{'rentmax'} && $rentmax>=$in{'rentmin'}) &&
>($pets=~/$in{'pets'}/ig || $in{'pets'} eq "Doesn't Matter" ||
>($in{'pets'}=~/yes/ig && ($pets=~/Cats/ig || $pets=~/Some Units/ig)))
>&& ($laundry=~/$in{'laundry'}/ig || $in{'laundry'} eq "Doesn't
>Matter" || ($in{'laundry'}=~/yes/ig && $laundry=~/Some Units/ig)) &&
>($garage=~/$in{'garage'}/ig || $in{'garage'} eq "Doesn't Matter" ||
>($in{'garage'}=~/yes/ig && $garage=~/Some Units/ig)))
> {$match="yes";
Wasn't that code a hassle to write?! Those /g modifiers there are useless
and are cargo-cult relics, I'm guessing. Please understand what the code
you're writing means and does.
if (
($in{location} eq 'All' or $location =~ /$in{location}/i)
and
($in{bedrooms} eq 'Any' or $bedrooms =~ /$in{bedrooms}/i or
($in{bedrooms} eq '4 plus Bedrooms' and
$bedrooms =~ /[456] Bedrooms/i)
)
and
($rentmin <= $in{rentmax} and $rentmax >= $in{rentmin})
and
# etc.
) {
$matches++;
# HTML STUFF HERE
}
And then all that icky HTML can be a here-doc, or a qq{} string, or a
template. But those "...\"...\"..." things have got to go.
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** Manning Publications, Co, is publishing my Perl Regex book **