Hi everyone,

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 }
The meat of the routine occurs within and just after the SET label, where I push the element [$hit] into @arrayA. Ideally, this should place into an array all elements $hit in the array that is the value to the $sitekey key that are >= $lowerlimit && <=$upperlimit, but for every time there's multiple elements of @arrayA, they are stored as identical, so something is wrong, and I assume it's a scoping problem, tho' I could be wrong.

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 ];
Any ideas, thoughts, or help are greatly welcomed and appreciated. Anyone who needs more info to help, please just ask me, and I tell you ev'ryt'ing.

Thanks,
Matt


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