> -----Original Message-----
> From: Kredler Stefan [mailto:[EMAIL PROTECTED]]
> Sent: Monday, December 17, 2001 7:20 AM
> To: '[EMAIL PROTECTED]'
> Subject: summarizing a list of numbers?
> 
> 
> Hello All,
> 
> I want to transform a sorted list into a compact list (if this is the
> correct term). 
> e.g. my list is 
> 
> 9,11,12,13,14,23,25,26,27,50  and want to have something like
> 9,11-14,23,25-27,50           (to pass on to a unix-command).
> 
> Is there any module or function I can use to summarize or 
> compact this?
> 
> Thanks for any hint.

Here's what I came up with. I wonder if this can be made shorter:

 @list = (9,11,12,13,14,23,25,26,27,50);

 while ($f = shift @list) {
     print $f;
     last unless @list;
     print(','), next unless $list[0] == ++$f;
     print "-";
     shift @list while @list > 1 && $list[1] == ++$f;
 }

Output:

 9,11-14,23,25-27,50

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to