Here's a function I wrote to group consecutive sets of integers in to a tabular form. #### ## ## my $table_hrf = consecutive( \@integers ) ## ## Description: function to find consecutive integers ## ## Parameters: a reference to an array of integers ## ## Returns: table of sorted, grouped integers in the form of ## ## base_integer => sequence_length ## ## For instance, the list ## ## qw( 20 2 3 4 5 6 7 23 19 17 25 30 11 12 22 21 68 103829 24 18 ); ## ## will sort to ## ## 2 => 5, ## 11 => 1, ## 17 => 8, ## 30 => 0, ## 68 => 0, ## 103829 => 0 ## ## which means ## ## sequence(5): 2 to 7 ## sequence(1): 11 to 12 ## sequence(8): 17 to 25 ## single: 30 ## single: 68 ## single: 103829 ## #### sub consecutive { my $integer_arf = shift; my %table = ( ); my $base = 0; my $previous = 0; foreach my $number ( sort numerically @{ $integer_arf } ) { if( ( $number - 1 ) == $previous ) # if the current number is one greater { # than the previous, increment our base $table{ $base }++; } else # we've found a new sequence { $table{ $number } = 0; # we're the base number, so set our adder to 0 $base = $number; } $previous = $number; # end of loop -- our $number is now old } return \%table; } sub numerically { $a <=> $b }