1: Here's a function I wrote to group consecutive sets of integers in to a tabular form.
2:
3: ####
4: ##
5: ## my $table_hrf = consecutive( \@integers )
6: ##
7: ## Description: function to find consecutive integers
8: ##
9: ## Parameters: a reference to an array of integers
10: ##
11: ## Returns: table of sorted, grouped integers in the form of
12: ##
13: ## base_integer => sequence_length
14: ##
15: ## For instance, the list
16: ##
17: ## qw( 20 2 3 4 5 6 7 23 19 17 25 30 11 12 22 21 68 103829 24 18 );
18: ##
19: ## will sort to
20: ##
21: ## 2 => 5,
22: ## 11 => 1,
23: ## 17 => 8,
24: ## 30 => 0,
25: ## 68 => 0,
26: ## 103829 => 0
27: ##
28: ## which means
29: ##
30: ## sequence(5): 2 to 7
31: ## sequence(1): 11 to 12
32: ## sequence(8): 17 to 25
33: ## single: 30
34: ## single: 68
35: ## single: 103829
36: ##
37: ####
38: sub consecutive
39: {
40: my $integer_arf = shift;
41:
42: my %table = ( );
43:
44: my $base = 0;
45: my $previous = 0;
46:
47: foreach my $number ( sort numerically @{ $integer_arf } )
48: {
49: if( ( $number - 1 ) == $previous ) # if the current number is one greater
50: { # than the previous, increment our base
51: $table{ $base }++;
52: }
53: else # we've found a new sequence
54: {
55: $table{ $number } = 0; # we're the base number, so set our adder to 0
56: $base = $number;
57: }
58:
59: $previous = $number; # end of loop -- our $number is now old
60: }
61:
62: return \%table;
63: }
64:
65: sub numerically { $a <=> $b }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: grouping consecutive integers
by hv (Prior) on Apr 15, 2003 at 16:13 UTC | |
|
Re: grouping consecutive integers
by artist (Parson) on Apr 15, 2003 at 16:29 UTC | |
|
Re: grouping consecutive integers
by Juerd (Abbot) on Apr 17, 2003 at 09:01 UTC | |
|
Re: grouping consecutive integers
by CountZero (Bishop) on Apr 18, 2003 at 18:53 UTC |