In search of elegant code: is variable is within a range???

D

David Filmer

Suppose I want to check if $foo is between 1 and 10. What I would like
to do is something elegant like:

if (1 < $foo < 10) {blah blah...

But Perl doesn't like that. Am I forced to resort to

if (1 < $foo && $foo < 10) {blah blah...

or is there a more elegant way to write it?
 
B

Bob Walton

David said:
Suppose I want to check if $foo is between 1 and 10. What I would like
to do is something elegant like:

if (1 < $foo < 10) {blah blah...

But Perl doesn't like that. Am I forced to resort to

if (1 < $foo && $foo < 10) {blah blah...

or is there a more elegant way to write it?

if(abs($foo-5.5)<4.5){blah blah...
 
B

Ben Morrow

if(abs($foo-5.5)<4.5){blah blah...

You clearly have a different idea of 'elegant' from me... :)

I would have said
if(grep /$foo/, 1..10) {
before that...

No, there isn't. Not till Perl6, anyway.

Ben
 
J

J. Gleixner

Ben said:
You clearly have a different idea of 'elegant' from me... :)

I would have said
if(grep /$foo/, 1..10) {

That'd handle 1<=$foo<=10 which is fine, just not what was needed. :)

if(grep /$foo/, 2..9) {

To be more readable, you could simply throw together a sub/method to do it.

sub is_between {
my ($v, $l, $h) = @_;
$l < $v && $v < $h;
}

if (is_between($foo, 1, 10)) {


See ya
 
J

Jay Tilton

(e-mail address removed) (David Filmer) wrote:

: Suppose I want to check if $foo is between 1 and 10. What I would like
: to do is something elegant like:
:
: if (1 < $foo < 10) {blah blah...
:
: But Perl doesn't like that. Am I forced to resort to
:
: if (1 < $foo && $foo < 10) {blah blah...
:
: or is there a more elegant way to write it?

Perl's core doesn't have an operator that can do that.

You can make one of your own, if you don't mind a bit of horror in your
elegance.

#!perl
use strict;
use warnings;

package Other;
sub new { bless[sort{$a<=>$b}@_[0,1]] }
use overload '==' => sub {$_[1]>$_[0][0] && $_[1]<$_[0][1]};
*main::between = \&new;

package main;
for my $foo ( 1 .. 10 ) {
print "$foo is between 1 and 10\n"
if $foo == between(1, 10);
}
 
J

Jeff 'japhy' Pinyan

sub is_between {
my ($v, $l, $h) = @_;
$l < $v && $v < $h;
}

if (is_between($foo, 1, 10)) {

That seems to be a horrible way to send the arguments to the function. If
the function's name is "between", I'd expect to send LOW, VAL, HIGH.

sub is_between { $_[0] < $_[1] and $_[1] < $_[2] }
if (is_between(1, $x, 10)) { ... }
 
M

Matt Garrish

Jeff 'japhy' Pinyan said:
That seems to be a horrible way to send the arguments to the function. If
the function's name is "between", I'd expect to send LOW, VAL, HIGH.

You should have read the documentation first...

Matt
 
M

Malcolm Dew-Jones

David Filmer ([email protected]) wrote:
: Suppose I want to check if $foo is between 1 and 10. What I would like
: to do is something elegant like:

: if (1 < $foo < 10) {blah blah...

: But Perl doesn't like that. Am I forced to resort to

: if (1 < $foo && $foo < 10) {blah blah...

: or is there a more elegant way to write it?


sub is { my $number = shift; bless \$number , 'Hairy' }

sub Hairy::between
{ my ($nref,$lo,$hi) = @_;
return ($lo < $$nref and $$nref < $hi);
}

my $foo = 5;

if ( is($foo)->between(1,10) )
{
print "Yes!";
}
 
R

Rafael Garcia-Suarez

Ben said:
You clearly have a different idea of 'elegant' from me... :)

I would have said
if(grep /$foo/, 1..10) {
before that...

This doesn't work at all. What if $foo is 2.47 ? What if $foo is ".1" ?
What if $foo is 0 ?
 
S

Sara

Suppose I want to check if $foo is between 1 and 10. What I would like
to do is something elegant like:

if (1 < $foo < 10) {blah blah...

But Perl doesn't like that. Am I forced to resort to

if (1 < $foo && $foo < 10) {blah blah...

or is there a more elegant way to write it?

....between 1 and 10, meaning 2..9?. That's what (1 < $foo && $foo <
10) would yield if it worked so I guess that's what you want. Anyhow
this might work:

$foo =~ /^0*[2-9]\.?.*$/

or if you know if $foo is always an integer,

$foo =~ /^0*[2-9]$/

or if you know there are no leading zeros:

$foo =~ /^[2-9]$/
 
B

Brad Baxter

David Filmer ([email protected]) wrote:
: if (1 < $foo && $foo < 10) {blah blah...
: or is there a more elegant way to write it?

sub is { my $number = shift; bless \$number , 'Hairy' }

sub Hairy::between
{ my ($nref,$lo,$hi) = @_;
return ($lo < $$nref and $$nref < $hi);
}

my $foo = 5;

if ( is($foo)->between(1,10) )
{
print "Yes!";
}

I am not worthy.

Brad
 

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,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top