mdunnbass has asked for the wisdom of the Perl Monks concerning the following question:
I've never really had a wonderful grasp of scoping, and it is seriously biting my digital ass at the moment. Any help would be greatly appreciated.
I am searching through the elements (all are numeric) of the terminal arrays of a HoHoA and trying to parse them into grouped sets in a new HoAoHoA, based on their proximity to each other. Unfortunately, the output I'm getting involves arrays of identical elements, and missing key/value pairs of hashes. For background info, the numerical array elements are the positions of hits to regex searches performed on ~500 Mb text files containing biological genome data separated into FASTA sequences.
Here's the code that's causing the problems. (Also - the print statements and Dumper are solely for debugging purposes. They will be removed once debugged.
FASTA: for (my $h=0;$h<(@fastarray);$h++) { $setscounter = 0; if (defined %{$matches{$fastarray[$h]}}){ SITE: for $site (sort {$a <=> $b } keys %{$matches{$fastarray[$h]}} +) { if (@{$matches{$fastarray[$h]}{$site}}) { $i = 0; ELEM: while ($i < length(@{$matches{$fastarray[$h]}{$site}})) { $lowerlimit = 0; my $low = $matches{$fastarray[$h]}{$site}[$i]; $lowerlimit = $low + 0; $upperlimit = $span + $lowerlimit; SITEKEY: for $sitekey (sort {$a <=> $b } keys %{$matches{$fasta +rray[$h]}}) { if (@{$matches{$fastarray[$h]}{$sitekey}}) { my @arrayA = (); my $hit = 0; SET: while ($hit < length(@{$matches{$fastarray[$h]}{$siteke +y}} )) { print "...in \$matches{$fastarray[$h]}{$sitekey}[$hit]\n"; if ($matches{$fastarray[$h]}{$sitekey}[$hit] >= $lowerlimi +t && $matches{$fastarray[$h]}{$sitekey}[$hit] <= $upperlimit) { push (@arrayA, $matches{$fastarray[$h]}{$sitekey}[$hit]) +; $hit++; next SET; #closes If setcount } elsif ($matches{$fastarray[$h]}{$sitekey}[$hit] < $lower +limit) { $hit++; next SET; } else {$hit++;} #closes while hit } 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 = (); } else { $sets{$fastarray[$h]}[$setscounter]{$sitekey} = undef; } $hit = 0; next SITEKEY; #closes if matches } next SITEKEY; #closes SITEKEY } $setscounter++; $i++; next ELEM; # closes while i } #closes if matches } next SITE; #closes for my site } #closes if defined } next FASTA; #closes for fastarray }
Here's some of the output, slightly annotated to explain what I really want:
...in $matches{>scaffold_446}{0}[1] $sets{>scaffold_446}[1] is: $VAR1 = '0'; $VAR2 = [ 0, # These 2 array elements should not BOTH 0 # be 0!!! What did I do wrong? ]; ...in $matches{>scaffold_446}{1}[0] ...in $matches{>scaffold_446}{2}[0] ...in $matches{>scaffold_446}{0}[0] ...in $matches{>scaffold_446}{0}[1] ...in $matches{>scaffold_446}{1}[0] $sets{>scaffold_446}[2] is: $VAR1 = '1'; # For each of these different '...in $matches..." $VAR2 = [ # There should be 6 $VAR variables. corresponding 40048 # to 3 key/value pairs, but I am doing something w +rong ]; # in my code, such that sometimes there are 3 + pairs, but $VAR3 = '0'; # often not. Any thoughts? $VAR4 = undef; ...in $matches{>scaffold_446}{2}[0] ...in $matches{>scaffold_446}{0}[0] ...in $matches{>scaffold_446}{0}[1] ...in $matches{>scaffold_446}{1}[0] ...in $matches{>scaffold_446}{2}[0] $sets{>scaffold_446}[3] is: $VAR1 = '1'; $VAR2 = undef; $VAR3 = '0'; $VAR4 = undef; $VAR5 = '2'; $VAR6 = [ 5468 ]; ...in $matches{>scaffold_3198}{0}[0] $sets{>scaffold_3198}[0] is: $VAR1 = '0'; $VAR2 = [ 1829 ];
Thanks,
Matt
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Scoping problems in nested loops
by GrandFather (Saint) on Nov 14, 2006 at 20:52 UTC | |
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 | |
|
Re: Scoping problems in nested loops
by jwkrahn (Abbot) on Nov 14, 2006 at 21:51 UTC | |
|
Re: Scoping problems in nested loops
by liverpole (Monsignor) on Nov 14, 2006 at 20:22 UTC | |
by mdunnbass (Monk) on Nov 14, 2006 at 20:39 UTC |