Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Comparing a value to a list of numbers

by haukex (Bishop)
on Jan 29, 2021 at 18:47 UTC ( #11127656=note: print w/replies, xml ) Need Help??


in reply to Comparing a value to a list of numbers

a variable, which contains a number to a list of numbers, such as 1,2,5,6,9,10,41-56

I'm a little unclear on what you mean. Is this variable an array, like the other monks have assumed? Because if it's a string, you'll have to parse it first. Luckily, there is a module that can handle a string such as the one you've shown, Set::IntSpan. The advantage of this module is that if the spans are large, like say 1,2,3-100000, it'll save a lot of memory as compared to an array or a hash. Otherwise, this is just a TIMTOWTDI solution.

use warnings; use strict; use Set::IntSpan; my $set = Set::IntSpan->new("1,2,5,6,9,10,41-56"); for my $x (1,2,3,42,100) { print "$x is ", $set->member($x)?"":"NOT ", "in the set\n"; } __END__ 1 is in the set 2 is in the set 3 is NOT in the set 42 is in the set 100 is NOT in the set

Replies are listed 'Best First'.
Re^2: Comparing a value to a list of numbers
by Bod (Chaplain) on Jan 29, 2021 at 23:25 UTC
    Luckily, there is a module that can handle a string such as the one you've shown, Set::IntSpan

    I'm not sure why I continue to be surprised...but...
    It still amazes me just how many really useful modules are shared in the Monastery every day!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2021-06-13 03:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (54 votes). Check out past polls.

    Notices?