develooper Front page | perl.module-authors | Postings from December 2003

Re: New module Algorithm::Interval2Prefix

Thread Previous | Thread Next
Fergal Daly
December 1, 2003 02:28
Re: New module Algorithm::Interval2Prefix
Message ID:
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
> 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


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


so instead of 

my @p = interval2prefix($lo, $hi);

my $found = 0;
foreach my $pref (@p)
	if ($num =~ /^$pref/)
		$found = 1;

if ($found)
	do stuff

you could just do

my $r = interval2regex($hi, $lo);

if ($num =~ /$r/)
	do stuff


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About