# Range of number

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi all -

I have a pre-determined low number and a pre-determined high number.
I simply need to know if my variable is within that range, and if so
print YES

I looked at Number::Range but thouhgt it was over kill.

Should I use a regex ?

Thx.

## Re: Range of number

O> Hi all - I have a pre-determined low number and a pre-determined
O> high number.  I simply need to know if my variable is within that
O> range, and if so print YES

O> I looked at Number::Range but thouhgt it was over kill.

O> Should I use a regex ?

Why not just use a conditional expression?

if (\$number <= \$MAXNUM && \$number >= \$MINNUM) .....

Charlton

--
Charlton Wilbur
cwilbur@chromatico.net

## Re: Range of number

Charlton Wilbur schreef:

It must be my math education showing through, but I always write that in
order:

if ( \$MINNUM <= \$number and \$number <= \$MAXNUM )

to emphasize what is between what.
I might make an exception if I know beforehand that any of the criteria
occurs more often, to profit from the shortcutting.

In Perl6 you can write

if ( \$MINNUM <= \$number <= \$MAXNUM )

--
Affijn, Ruud

"Gewoon is een tijger."

## Re: Range of number

Dr.Ruud wrote:

While I can see the convience in that, I can also see people slipping on
this, as most other languages (including Perl 5), would parse that by
first evaulating a boolean result from (\$MINNUM <= \$number) and then
(result <= \$MAXNUM)... I don't know if any real code actually depends on
that behavior, but Perl 6 does seem to make that more useful.

Any word on such a construct making it's way into Perl 5 (5.12 perhaps?)
?

--
szr

## Re: Range of number

_
szr (szrRE@szromanMO.comVE) wrote on VCCCLXXIX September MCMXCIII in
][  Dr.Ruud wrote:
][ >
][ > In Perl6 you can write
][ >
][ >   if ( \$MINNUM <= \$number <= \$MAXNUM )
][
][  While I can see the convience in that, I can also see people slipping on
][  this, as most other languages (including Perl 5), would parse that by
][  first evaulating a boolean result from (\$MINNUM <= \$number) and then
][  (result <= \$MAXNUM)... I don't know if any real code actually depends on
][  that behavior, but Perl 6 does seem to make that more useful.

Actually, in Perl 5 isn't like "most other languages", as in Perl 5,
chained comparisons are a syntax error:

perl -e 'print if 1 < 2 < 3'

So I highly doubt any Perl 5 programmer 'slipping up'.

As for other languages, IIRC, chained comparisons have always worked the
way they will in Perl 6.

][  Any word on such a construct making it's way into Perl 5 (5.12 perhaps?)
][  ?

Considering it's currently a syntax error, I doubt there will be much
resistance if someone provides a patch.

But as long as noone provides a patch, it's highly unlike to find its way
in any version of Perl 5.

Abigail
--
echo "==== ======= ==== ======"|perl -pes/=/J/|perl -pes/==/us/|perl -pes/=/t/\
|perl -pes/=/A/|perl -pes/=/n/|perl -pes/=/o/|perl -pes/==/th/|perl -pes/=/e/\
|perl -pes/=/r/|perl -pes/=/P/|perl -pes/=/e/|perl -pes/==/rl/|perl -pes/=/H/\
|perl -pes/=/a/|perl -pes/=/c/|perl -pes/=/k/|perl -pes/==/er/|perl -pes/=/./;

## Re: Range of number

[...]

Perl 6 will be the first language I know of that will do chained comparisions.
Most others do them wrong.  For example in C

(2<1<3)

Will return true, while in other languages such a chained comparison is a
type error.

## Re: Range of number

_
David Formosa (aka ? the Platypus) (dformosa@usyd.edu.au) wrote on
VCCCLXXX September MCMXCIII in
""
""  [...]
""
""
"" > As for other languages, IIRC, chained comparisons have always worked the
"" > way they will in Perl 6.
""
""  Perl 6 will be the first language I know of that will do chained
comparisions.
""  Most others do them wrong.  For example in C
""
""      (2<1<3)
""
""  Will return true, while in other languages such a chained comparison is a
""  type error.

Right. What I wanted to say was:

As for other languages, IIRC, *IN PYTHON*, chained comparisons
have always worked the way they will in Perl 6.

Abigail
--
A perl rose:  perl -e '@}-`-,-`-%-'

## Re: Range of number

sub in_range { \$_[2] <= \$_[1] and \$_[1] <= \$_[0] }

print "Yes\n" if in_range( \$MAXNUM => \$number => \$MINNUM );

--

## Re: Range of number

I wouldn't.

What's wrong with:

my ( \$low, \$high ) = ( 1, 10 );
my \$num = shift;

# check if \$num is defined, and a number, etc..
print "YES\n" if ( \$num >=3D \$low && \$num <=3D \$high );

## Re: Range of number

Of course not.

regex are for processing strings.

Your data are numbers, not strings.

Perl has numeric comparison operators.

You should use numeric operators with numeric data.

--
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

## Re: Range of number

One wrote:

if ( \$x < \$hi and \$x > \$low ) {
print "YES\n";
}

Or am I not understanding your question?

## Re: Range of number

One schrieb:

\$ perl -nle '\$n=\$_;print+(grep\$n==\$_=>17..4217)?"YES":"NO"'
3
NO
100
YES
4217
YES
4218
NO

;-) Mirko