in reply to Scoping problems in nested loops

You have a big glaring bug here:
if (@arrayA && $hit == length(@{$matches{$fastarray[$h]}{$si +tekey}})) { $sets{$fastarray[$h]}[$setscounter]{$sitekey} = \@arrayA; print "\$sets{$fastarray[$h]}[$setscounter] is:\n"; print Dumper(%{$sets{$fastarray[$h]}[$setscounter]}); @arrayA = (); }
You are assigning a reference to @arrayA and then clearing out the contents of @arrayA so it is the same as assigning an empty anonymous array in the first place. Remove the line: "@arrayA = ();". Anyway, it looks like your code could be simplified to:
for my $fastarray ( @fastarray ) { my $hash = $matches{ $fastarray }; for my $site ( sort { $a <=> $b } keys %$hash ) { for my $lowerlimit ( @{ $hash->{ $site } } ) { my $upperlimit = $span + $lowerlimit; for my $sitekey ( sort { $a <=> $b } keys %$hash ) { push @{ $sets{ $fastarray } }, { $sitekey => undef }; my @arrayA = grep { $_ >= $lowerlimit && $_ <= $upperl +imit } @{ $hash->{ $sitekey } } or next; $sets{ $fastarray }[ -1 ]{ $sitekey } = \@arrayA; print "\$sets{$fastarray}[-1] is:\n", Dumper $sets{ $f +astarray }[ -1 ]; } } }