Eli the Bearded <*@eli.users.panix.com> writes:
> In comp.lang.perl.misc, Mr P <> wrote:
>> I read up on this on the www and I found ideas like
>>
>> if ( /\b([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\b/ ) ...
>>
>> which is pretty uncipherable at a glance and just in general not
>> elegant in any sense.
>
> True. That's why it's much better to not use regexps for numerical
> ranges.
>
>> I generally do something like
>>
>> if ( /(\d+)/ && $1 > 256 && $1 < 1024 )
>
> I'd write that as
>
> if ( /(\d+)/ && ($1 > 256) && ($1 < 1024) )
>
> because I like to make sure things operate in the order I want them
> to.
Really?
First off, I hope you're aware that both forms are exactly
equivalent., since "<" binds more tightly than "&&", and "&&"
imposes a left-to-right evaluation with or without the parentheses.
An argument for using the extra parentheses would be that they make
it clearer. They don't for me personally; in this particular case,
the precedence is carved deeply enough into my brain that it's clear
enough without the parentheses. But YMMV. Obviously, different
people have different levels of comfort with the precedence levels
of the various operators.
But I'd write it as:
if (/(\d+)/ and $1 > 256 and $1 < 1024)
I usually prefer "and" and "or" over "&&" and "||". On the other
hand, I have been bitten a few times by the *low* precedence of
"and" and "or"; I've mistakenly written things like
return $this and $that;
which never evaluates $that.
(And none of these are equivalent to the original regexp, which
checks for values from 0 to 255.)
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"