Ok, I think I see what you are trying to do. Here is one way of many to code this sort of thing:
#!usr/bin/perl use strict; use warnings; my $nHeaderLines = 2; my $nDataLinesPerFile = 4; my @header; # the "Big File" is the DATA segment below, # maybe millions of lines... for (1..$nHeaderLines) # read header lines from big file { my $header_line = <DATA>; push @header, $header_line; } # divide the big file data into smaller files, # each with the initial header... my $nFile = 0; my $fileNameBase = "SmallerFile"; my $nDataLine = 99999; my $line; while ($nDataLine++, defined ($line = <DATA>)) { if ($nDataLine > $nDataLinesPerFile) # start new file { $nFile++; my $name = "$fileNameBase$nFile.txt"; open (OUT, '>', "./$name") or die "$!"; print OUT @header; $nDataLine=1; } print OUT $line; } print "Program Done!\n"; __DATA__ Header 1 Header 2 data 1 data 2 data 3 data 4 data 5 data 6 data 7 data 8 data 9
PS: This code has no advance knowledge of how many smaller files will be created. Nothing like "34 files" is hard coded.
The code creates "as many files as are needed". In the case above, there are 3 files. data lines 1,2,3,4 in one file, data lines 5,6,7,8 in another and the last 9th data line in a third file.

In reply to Re^3: How to restart a while loop that is iterating through each line of a file? by Marshall
in thread 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.