Esteemed colleagues: I have written a little program to extract relevant lines out of data files and format them nicely, as shown:

#!/usr/bin/perl use warnings; use strict; # Formats a PennCNV output file to remove extraneous information. Also + removes # CNVs containing less than 5 consecutive SNPs. Invoke this program wi +th the name # of the file to be modified. # Open input file and create output file, adding suffix '.truncated' t +o filename: open( my $file, '<', $ARGV[0] ) or die "Cannot open for reading, $!"; open( my $out, '>', $ARGV[0] . '.truncated' ) or die "Cannot open for writing, $!"; # Print headings to output file: printf $out "%-28s %-30s %-12s %-10s %-18s %-22s %-21s %s \n\n", "Samp +le I.D.", "Chromosome & coordinates", "Copy number", "No. SNPs", "CNV length (bp +)", "First SNP", "Last SNP", "Overlapping Gene(s)"; # Loop that matches each valid line of input file, using capturing par +entheses to # isolate each separate field. We also use '/x' flag to allow arbitrar +y whitespace # so we can break the regular expression up and add comments for reada +bility: while (<$file>) { if ( / (chr\d+:\d+-\d+) # $1 Chromosome & coordinates \s+ (numsnp=\d+) # $2 Number of SNPs \s+ (length=\S+) # $3 CNV length (bp) \s+ (state\d+) # $4 HMM state , (cn=\d+) # $5 Copy number \s+ (\S+) # $6 File directory \/ (\S+) # $7 Sample I.D. \s+ (startsnp=rs\d+) # $8 First SNP in CNV \s+ (endsnp=rs\d+) # $9 Last SNP in CNV \s+ (\S+) # $10 Gene(s) overlapping CNV \s+ (\S+) # $11 Distance of gene(s) from CNV /x and ( !/numsnp=[1-4]\s+/ ) # we also ignore CNVs with less tha +n 5 SNPs ) { # Print each line to output file using left-justified formatting: printf $out "%-28s %-30s %-12s %-10s %-18s %-22s %-21s %s \n", $7, $1, $5, $2, $3, $8, $9, $10; } } # Close the open filehandles: close($file); close($out);

My problem arises because I want to modify the program to write the rejected lines to a separate file, but when I try to use the capturing parentheses in an 'else' block inside the while loop right after the 'if', I end up with a blank file and perl gives me lots of errors relating to the $1, $2 etc. being out of scope.

So I'd like to know if there's an easy way of diverting any lines that don't match the 'if' patterns into a separate file.

Also, if anyone can spot other potential issues with my code, I'd appreciate constructive criticism. Thanks.


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