in reply to Print series of numbers

I don't know if you consider this an improved version, it is at least a bit different:

my ($min,$max,@newlist); @list = sort { $a <=> $b } @list; $min=$max=shift @list; while (@list) { $_= shift @list; if (not $_ == ++$max) { --$max; push @newlist, "$min-$max"; $min=$max=$_; } } push @newlist, "$min-$_" if (defined $_); print join(',',map {s/(\d+)-\1/$1/} @newlist);

UDPATE: Corrected two bugs, a missing '=' and it didn't print the last value or range.

Replies are listed 'Best First'.
Re^2: Print series of numbers
by BioLion (Curate) on Oct 20, 2009 at 10:40 UTC

    ++! I was kicking myself in the head trying to get some sort of regex | s/// based thing to work. I didn't want to cheat and look at the codegolf page...

    Just a something something...