[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Formatting integer sets with ranges



Peter Scott wrote:
> 
> $_ = '1,2,3,5,6,7,8,9,10,11,12,13,14,16,17,19,20,21';
> 
> s/(\d+),\d+,(\d+)/$2==$1+2?"$1-$2":$&/eg;
> s/-(\d+),(\d+)/$2==$1+1?"-$2":$&/eg;
> 1 while s/-\d+(-\d)/$1/g;
> 
> print "$_\n";
> 
> 1-3,5-14,16,17,19-21
> 
> Has the property of not hyphenating itmes differing only by 1, a pet peeve
> of mine.
> 

1,2,3,4,5,8,9,10 => 1-4,5,8,9,10   ????

That three-at-a-time probably needs a "1 while ..."

Rick

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe