or download this
@start = (1,2,8,9,10,11,12,43,66,109,121,122,123,657);
@queue = ();
...
push @output , (@queue > 3) ? $queue[0] . '-' . $queue[-1] : join( ','
+ , @queue );
print 'Expected :: 1,2,8-12,43,66,109,121-123,657' , "\n";
print 'End :: ' , join(',', @output);
or download this
start :: 1,2,8,9,10,11,12,43,66,109,121,122,123,657
Expected :: 1,2,8-12,43,66,109,121-123,657
End :: 1,2,8-12,43,66,109,121-123,657