in reply to Scoping problems in nested loops
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 ;).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Scoping problems in nested loops
by mdunnbass (Monk) on Nov 15, 2006 at 15:25 UTC | |
by GrandFather (Saint) on Nov 15, 2006 at 18:48 UTC | |
by mdunnbass (Monk) on Nov 15, 2006 at 20:32 UTC | |
by GrandFather (Saint) on Nov 15, 2006 at 21:30 UTC |