in reply to Check for sequential values in an array that can contain duplicate int values

rehgb,

Here's one way to do it:

my @a=qw(1 2 3 4 6 3 1); my %h = map {$_=>1} @a; my ($max) = reverse sort keys %h; for (1..$max) { print "gap at $_ \n" unless exists $h{$_} }
hth,
andy.
  • Comment on Re: Check for sequential values in an array that can contain duplicate int values
  • Download Code

Replies are listed 'Best First'.
Re: Answer: Check for sequential values in an array that can contain duplicate int values
by blakem (Monsignor) on Dec 17, 2001 at 03:47 UTC
    I have two issues with this line:
    my ($max) = reverse sort keys %h;
    The first is that sort defaults to string sorting (i.e. cmp) which will mess up your order as soon as you get to 10. A better way to write it is:
    my ($max) = sort {$b <=> $a} keys %h;
    The second issue is that sorting is an expensive way to find the maximum element. Granted its not bad for small numbers (and this example uses small numbers indeed) To find the maximum number in a set of positive reals, I would use:
    my $max = 0; $max < $_ and $max = $_ for @a;
    This finds the maximum element in one pass through the data.

    -Blake