On Sun, Nov 30, 2003 at 05:17:10PM +0100, Lars Thegler wrote:
> Hi all,
>
> I've written a small module, encapsulating an algorithm that can generate a
> set of 'prefixes' (patterns that match the beginning of a numeric string)
> from an 'interval' (range) of integers. This is a problem often occurring
> working with telephony switching equipment or IP address subnetting.
>
> I've trawled CPAN to locate prior work with similar functionality, but to no
> avail.
>
> The POD is attatched below, and the module distfile can be fetched from
>
> http://lars.thegler.dk/perl/Algorithm-Interval2Prefix-0.01.tar.gz
>
> Question: Am I reinventing something here?
I never saw it before.
> Question: Is the namespace appropriate?
Looks ok to me although I'd prefer "To" rather than "2".
> Comments on code, style etc are welcome.
> Taking an interval as input, this module will construct the smallest set
> of prefixes, such that all numbers in the interval will match exactly
> one of the prefixes, and no prefix will match a number not in the
> interval.
You need to say something about the length of the number because as it
3000-3999 produces just 3 and there are a lot of numbers that start with 3
but which aren't in the interval. In the same vein, a mode that produces
a set of strings like
^3\d{3}$
which can be used directly with Perl's re-engine might be useful or even
something that turns
2900-3999 into a single regex string which will match if and only
if the number is in any of the intervals
^(?:29\d{2}|3\d{3})$
so instead of
my @p = interval2prefix($lo, $hi);
my $found = 0;
foreach my $pref (@p)
{
if ($num =~ /^$pref/)
{
$found = 1;
last;
}
}
if ($found)
{
do stuff
}
you could just do
my $r = interval2regex($hi, $lo);
if ($num =~ /$r/)
{
do stuff
}
F