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

Discussion in 'Perl Misc' started by David Filmer, Dec 3, 2003.

  1. David Filmer

    David Filmer Guest

    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?
    David Filmer, Dec 3, 2003
    #1
    1. Advertising

  2. David Filmer

    Bob Walton Guest

    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?
    >


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

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
    Bob Walton, Dec 3, 2003
    #2
    1. Advertising

  3. David Filmer

    Ben Morrow Guest

    d wrote:
    > 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?
    > >

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

    --
    Every twenty-four hours about 34k children die from the effects of poverty.
    Meanwhile, the latest estimate is that 2800 people died on 9/11, so it's like
    that image, that ghastly, grey-billowing, double-barrelled fall, repeated
    twelve times every day. Full of children. [Iain Banks]
    Ben Morrow, Dec 3, 2003
    #3
  4. David Filmer

    Ben Morrow Guest

    Ben Morrow <> wrote:
    > I would have said
    > if(grep /$foo/, 1..10) {
    > before that...


    Yeah, OK, not thinking... I've got integers on the brain :(.

    Sorry...

    Ben

    --
    If you put all the prophets, | You'd have so much more reason
    Mystics and saints | Than ever was born
    In one room together, | Out of all of the conflicts of time.
    |----------------+---------------| The Levellers, 'Believers'
    Ben Morrow, Dec 3, 2003
    #4
  5. David Filmer

    J. Gleixner Guest

    Ben Morrow wrote:
    > d wrote:
    >
    >>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?
    >>>

    >>
    >> 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) {


    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. Gleixner, Dec 3, 2003
    #5
  6. David Filmer

    Jay Tilton Guest

    (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);
    }
    Jay Tilton, Dec 4, 2003
    #6
  7. On Wed, 3 Dec 2003, J. Gleixner wrote:

    >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)) { ... }

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
    Jeff 'japhy' Pinyan, Dec 4, 2003
    #7
  8. David Filmer

    Matt Garrish Guest

    "Jeff 'japhy' Pinyan" <> wrote in message
    news:p...
    > On Wed, 3 Dec 2003, J. Gleixner wrote:
    >
    > >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.
    >


    You should have read the documentation first...

    Matt
    Matt Garrish, Dec 4, 2003
    #8
  9. 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?


    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!";
    }
    Malcolm Dew-Jones, Dec 4, 2003
    #9
  10. Ben Morrow wrote:
    >> 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...


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

    --
    Unfinished is not *NIX
    Rafael Garcia-Suarez, Dec 4, 2003
    #10
  11. David Filmer

    Sara Guest

    (David Filmer) wrote in message news:<>...
    > 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]$/
    Sara, Dec 4, 2003
    #11
  12. David Filmer

    Brad Baxter Guest

    On Wed, 3 Dec 2003, Malcolm Dew-Jones wrote:

    > David Filmer () 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
    Brad Baxter, Dec 5, 2003
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abby Lee
    Replies:
    5
    Views:
    375
    Abby Lee
    Aug 2, 2004
  2. David Filmer
    Replies:
    9
    Views:
    105
  3. David Filmer

    In search of elegant code: inverting a string

    David Filmer, Oct 29, 2003, in forum: Perl Misc
    Replies:
    9
    Views:
    114
    Russ Jones
    Oct 30, 2003
  4. David Filmer
    Replies:
    8
    Views:
    113
    geoffroy
    Dec 14, 2003
  5. David Filmer
    Replies:
    3
    Views:
    144
    Heinrich Mislik
    Sep 22, 2004
Loading...

Share This Page