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

Re: New module Algorithm::Interval2Prefix

Thread Previous | Thread Next
From:
Fergal Daly
Date:
December 2, 2003 13:59
Subject:
Re: New module Algorithm::Interval2Prefix
Message ID:
200312022159.55230.fergal@esatclear.ie
On Tuesday 02 December 2003 20:41, Lars Thegler wrote:
> On Monday, December 01, 2003 11:24 AM, Fergal Daly wrote:
> Obviously, if the numbers are of variable length, then we have a different
> situation, that cannot easily be handled this way. Maybe I should check $lo
> and $hi to ensure they are the same length...

It would definitely be a good idea to check that but I was pointing out that 
the documentation doesn't actually say that the hi and lo must be the same 
length and also, the numbers you are testing it against must be that length 
also.

> > 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})$
> 
> Intersting proposistion. Assuming that $lo and $hi are the same length, then
> doing
> 
>   $n = length($lo)-length($_);
>   $p .= $n ? "\d{$n}" : '';
> 
> to each prefix should do the trick. I'll be adding that to the module.

Exactly. Actually, if you're going to put in the \d{$n} stuff then you can  
make this work for arbitrary intervals without any constraints on the length 
of hi, lo and the number being tested (you must also include ^ and $ in the 
regex for this to work). So

i2p(900, 1999) would give back

^(?:9\d{2}|1\d{3})$

and if your algorithm was really clever

i2p(500,5000) would give

^(?:[5-9]\d{2}|[1-5]\d{3})$

but that may not be worth the effort. Also, the regexes involving \d{$n} will 
actually be slower for your telephone application because you already know 
that the number is the right length,

F


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About