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

Re: [FWP] Formatting integer sets with ranges



Here's a somewhat more "pictorial" way:

    @nums = split /,/, '1,2,3,5,6,7,10,12,13'; #sorted

    $last = $range = shift @nums;

    for $curr (@nums) {
      if ($last == $curr-1) {
        $range =~ s/-$last$/-$curr/ || $range =~ s/(,?$last)$/$1-$curr/
      } 
      else {
        $range .= ",$curr"
      }
      $last = $curr
    }

    print $range;

--
Tushar Samant
~
~
:wq


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