in reply to Re: Check for sequential values in an array that can contain duplicate int values
in thread Check for sequential values in an array that can contain duplicate int values
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) = reverse sort 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) = sort {$b <=> $a} keys %h;
This finds the maximum element in one pass through the data.my $max = 0; $max < $_ and $max = $_ for @a;
-Blake
|
|---|