in reply to Capturing parentheses out of scope
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.
|
---|