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

Re: [FWP] Formatting integer sets with ranges



$_ = '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.

--
Peter Scott
Pacific Systems Design Technologies


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