When the second match is successful, it's clobbering the captures from the first match. So save the captures of the first match before attempting the second.

Furthermore, the structure you mention makes no sense. You'd be using $1, etc even when the first match op didn't match.

Fix:

while (<$file>) { if (my @captures = / (chr\d+:\d+-\d+) # 0 Chromosome & coordinates \s+ (numsnp=\d+) # 1 Number of SNPs \s+ (length=\S+) # 2 CNV length (bp) \s+ (state\d+) # 3 HMM state , (cn=\d+) # 4 Copy number \s+ (\S+) # 5 File directory \/ (\S+) # 6 Sample I.D. \s+ (startsnp=rs\d+) # 7 First SNP in CNV \s+ (endsnp=rs\d+) # 8 Last SNP in CNV \s+ (\S+) # 9 Gene(s) overlapping CNV \s+ (\S+) # 10 Distance of gene(s) from CNV /x) { if ( /numsnp=[1-4]\s+/ ) { ... } else { printf $out "%-28s %-30s %-12s %-10s %-18s %-22s %-21s %s\ +n", @captures[6,0,4,1,2,7,8,9]; } } }

But why are you using a regex match to do a numerical comparison on a value you already have? Fix:

while (<$file>) { if (my @captures = / (chr\d+:\d+-\d+) # 0 Chromosome & coordinates \s+ (numsnp=\d+) # 1 Number of SNPs \s+ (length=\S+) # 2 CNV length (bp) \s+ (state\d+) # 3 HMM state , (cn=\d+) # 4 Copy number \s+ (\S+) # 5 File directory \/ (\S+) # 6 Sample I.D. \s+ (startsnp=rs\d+) # 7 First SNP in CNV \s+ (endsnp=rs\d+) # 8 Last SNP in CNV \s+ (\S+) # 9 Gene(s) overlapping CNV \s+ (\S+) # 10 Distance of gene(s) from CNV /x) { if ( $captures[1] >= 1 && $captures[1] <= 4 ) { ... } else { printf $out "%-28s %-30s %-12s %-10s %-18s %-22s %-21s %s\ +n", @matches[6,0,4,1,2,7,8,9]; } } }

This last snippet avoids the original problem, so you could go back to using $1 and such, but that would go back to needlessly using global variables.


In reply to Re: Capturing parentheses out of scope by ikegami
in thread Capturing parentheses out of scope by iangibson

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.