Greetings monks,
I am currently trying to solve a loop riddle that has bested me.
The problem: I am trying to take the first 16 lines of a single file, and populate those same 16 lines in 34 different (but incrementally named) files. My current code:

my $filename = "../../Annotation_output"; for (my $i=0; $i<=33; $i++) { my $file_count = $i; open(my $new_fh, '>>', ("$filename" . "$file_count" . ".vcf")); my $count = 0; while (<$fh>) { print $new_fh "$_"; $count++; if ($count == 16) { last; } } }
$fh is the file im trying to take the 16 lines from, FYI

I have had the first file (Annotation_output0.vcf) gain the 16 lines needed, but never any files afterward.

Common outputs have included having each of the 34 files gain 16 lines with the while loop never reseting (So Annotation_output0.vcf = lines 0-15, Annotation_output1.vcf = lines 16-31, Annotation_output2.vcf = lines 32-47, and so on), as well as seeing just output0.vcf gain 16 lines while output1.vcf gains the entirety of the while loop, never advancing past $i = 2

Any help is appreciated!


In reply to How to restart a while loop that is iterating through each line of a file? by cookersjs

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.