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 (Parson) on Jan 29, 2021 at 23:25 UTC |