#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    342
    Rep Power
    12

    Check if number is in range 23-98


    If i have a range "23-98,103-150", is there a better way to write than my coding?

    Code:
    $range = "23-98,103-150";
    $number = "110";
    
    @array = split(",",$range);
    $foundit = 0;
    foreach my $one (@array) {
     if ($one =~ /\-/) {
       my ($start,$end) = split("-",$one);
        if ($start <= $number && $number <= $end) {
          $foundit = 1;
           last;
       } 
     } elsif ($one == $number) {
        $foundit = 1;
        last;
     }
    }
    Last edited by tommy916; December 1st, 2008 at 09:36 PM.
  2. #2
  3. intellectually challenged
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2004
    Location
    m-w.com
    Posts
    662
    Rep Power
    406
    This might be easier. Number::Range

    Perl Code:
    use Number::Range;
     
    my $range = Number::Range->new("23..98,103..150");
         if ($range->inrange("110")) {
             print "In range\n";
         } else {
             print "Not in range\n";
         }
    ...Working with a limited amount of intelligence.
    --Kevin
  4. #3
  5. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2004
    Location
    Sunny Southern California
    Posts
    2,687
    Rep Power
    862
    If you must start with your data in strings:

    $range = "23-98,103-150";
    $number = "110";

    then your code is OK. You might want to look into the module my bro posted above.

IMN logo majestic logo threadwatch logo seochat tools logo