in reply to Consecutive number checking
This is quick-n-dirty, and works just fine...
use strict; use warnings; my @array = (1,5,6,7,8,55,63,77,89,103,104,105,106,200,215,554); local $, = ', '; print sort { $a <=> $b } keys %{ { map { ( $array[ $_ ] == $array[ $_ + 1 ] - 1 ) ? ( $array[ $_ ] => 0, $array[ $_ + 1 ] => 0 ) : () } 0 .. $#array - 1 } };
It does assume that @array is pre-sorted, and contains no duplicates. I was trying to come up with a regexp solution but gave up after confusing myself with (??{...}) constructs. ;) ...Enjoy.
Updated: Discarded unnecessary use of a named hash. ;)
Dave
|
|---|