Hi Monks,

I've written a short script which loops through a file, using some information from a specific line to extract a section of the file using an external program. This section (called $mafBlock) is then used in another external program, and it is the output of this program (called m2.gff) that I am interested in. However, each time the loop proceeds the output file m2.gff is overwritten, so I thought one solution would be to try and open this file within the loop, and then print the lines I require to another output file (m2_most-cons.gff). If I do this I get a syntax error for opening this second filehandle, I've tried a few different ways of writing this but none seem to work. My code is:

use warnings; use strict; my $maf = "input.maf"; open (IN1, "input.maf") or die; open (OUT1, ">m2.maf") or die; open (OUT2, ">m2_most-cons.gff") or die; while (my $line1 = <IN1>) { chomp $line1; if ($line1 =~ /^s\sficAlb2/) { my @columns = split(/\t/, $line1); my $start = $columns[2] + 1; my $end = $columns[2] + $columns[3]; my $chrom = $columns[1]; my $mafBlock = qx(maf_parse --start $start --end $end $maf); print OUT1 "$mafBlock1\n"; my $phastCons = qx(phastCons --target-coverage 0.3 --expected- +length 45 --rho 0.31 --most-conserved m2.gff --seqname $chrom --msa-f +ormat MAF m2.maf nonconserved-all-4d.mod); print "$phastCons\n" open (IN2, "m2.gff") or die; while (my $line2 = <IN2>) { if ($line2 =~ /^fic/) { print OUT2 "$line2\n"; } } } open (OUT1, ">m2.maf") or die; close IN2; } close IN1; close OUT1; close OUT2; exit;

Apologies if this is something completely wrong, I'm new to coding and this seemed the most intuitive way to collect the output required.

Many thanks in advance!

In reply to Error opening file handle within while loop by rjc33

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.