Consider this example code:

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; } } }

Prints:

1 - 12: [0 .. 11]: 1 2 3 4 5 6 7 8 9 10 11 12 10 - 20: [9 .. 14.5]: 10 11 12 13 14 15 18 - 25: [15 .. 19]: 21 22 23 24 25 2000 - 3000: [1895 .. 1901.5]: 2000 2010 2011 2012 2013 2014 2015

In your context you would do something like:

my $lowIndex = BinSearch ($lowerlimit, sub {$_[0] <=> $_[1]}, $matches +{$element}{$sitekey}) + 0.5; my $highIndex = BinSearch ($upperlimit, sub {$_[0] <=> $_[1]}, $matche +s{$element}{$sitekey}); my @arrayA = @{$matches{$element}{$sitekey}}[$lowIndex..$highIndex];

DWIM is Perl's answer to Gödel

In reply to Re^5: Scoping problems in nested loops by GrandFather
in thread Scoping problems in nested loops by mdunnbass

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.