length(@{$matches{$element}{$site}} looks rather surprising to me. Consider:

my @array = ('bannana', 'apple', 'orange'); print length (@array);

Prints '1'. Most likely what you really wanted was the number of elements in the array. That is simple @array in a scalar context. However is seems like you really want to iterate over the array so a beter construct would be:

for my $low (@{$matches{$element}{$site}}) {

and omit the following line initialising $low. There are a number of places where the same thing seems to have been done.

Other changes I'd make involve early exits from loops rather than nesting inside if statements, removing duplicated code, removing superfluious nexts, using Perl for loops rather than C for loops and adding a little vertical whitespace to make flow clearer. At the end of that process I get:

my @fastarray; my %matches; my %sets; my $span; for my $element (@fastarray) { my $setscounter = 0; next unless defined %{$matches{$element}}; for my $site (sort {$a <=> $b } keys %{$matches{$element}}) { next unless @{$matches{$element}{$site}}; for my $low (@{$matches{$element}{$site}}) { my $lowerlimit = $low + 0; my $upperlimit = $span + $lowerlimit; for my $sitekey (sort {$a <=> $b } keys %{$matches{$elemen +t}}) { next unless @{$matches{$element}{$sitekey}}; my @arrayA = (); for my $hElem (@{$matches{$element}{$sitekey}}) { print "...in \$hElem\n"; if ($hElem >= $lowerlimit && $hElem <= $upperlimit +) { push (@arrayA, $hElem); } } if (@arrayA) { $sets{$element}[$setscounter]{$sitekey} = \@arrayA +; print "\$sets{$element}[$setscounter] is:\n"; print Dumper(%{$sets{$element}[$setscounter]}); @arrayA = (); } else { $sets{$element}[$setscounter]{$sitekey} = undef; } } $setscounter++; } } }

which may or may not be nothing at all like what you intended ;).


DWIM is Perl's answer to Gödel

In reply to Re: 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.