Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Matching data against non-consecutive range

by lidden (Curate)
on Jan 27, 2005 at 05:48 UTC ( [id://425470]=note: print w/replies, xml ) Need Help??


in reply to Matching data against non-consecutive range

[] does not do what you think it does in a regexp. I would do a sub to do what you want done.
# Zone 8 my $z8 = "10-374,376-379,382-385,388-499,530-534,541-543,618,619,700-7 +04,707-709"; my $num = "375"; print "Found it!\n" if in_range($z8, $num); #$num =~/[$z8]/; sub in_range{ my $numbers = shift; my $num = shift; my @ranges = split ',', $numbers; foreach my $r (@ranges){ if (my ($small, $big) = $r =~ /^(\d+)-(\d+)$/){ return 1 if $small <= $num and $big >= $num; } elsif( my ($n) = $r =~ /^(\d+)$/){ return 1 if $n == $num; } else{ die 'Oh noo:-('; } } return 0; }
Update: Fixed bugs

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://425470]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-23 13:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found