use strict; use warnings; my @bigarray = ( # Think @{$matches{$element}{$sitekey}} 1 .. 15, 21 .. 100, 200 .. 2000, 2010 .. 2015 ); test (1, 12); test (10, 20); test (18, 25); test (2000, 3000); sub test { my $lowIndex = BinSearch ($_[0], \&cmpFunc, \@bigarray) + 0.5; my $highIndex = BinSearch ($_[1], \&cmpFunc, \@bigarray); my @arrayA = @bigarray[$lowIndex..$highIndex]; print "$_[0] - $_[1]: [$lowIndex .. $highIndex]: @arrayA\n"; } sub cmpFunc { $_[0] <=> $_[1]; } sub BinSearch { my ($target, $cmp, $array) = @_; my $posmin = 0; my $posmax = $#$array; return -0.5 if $cmp->($array->[0], $target) > 0; return $#$array + 0.5 if &$cmp ($array->[-1], $target) < 0; while (1) { my $mid = int (($posmin + $posmax) / 2); my $result = $cmp->($array->[$mid], $target); if ($result < 0) { $posmin = $posmax, next if $mid == $posmin && $posmax != $posmin; return $mid + 0.5 if $mid == $posmin; $posmin = $mid; } elsif ($result > 0) { $posmax = $posmin, next if $mid == $posmax && $posmax != $posmin; return $mid - 0.5 if $mid == $posmax; $posmax = $mid; } else { return $mid; } } }