If you are just interested in a fixed number of lines per record you can:

use strict; use warnings; my $num = 1; my $recLines = 2; while (!eof DATA) { ## Open the file once every iteration my $outfile = sprintf("Int_%s.txt", $num); print "---> Create file $outfile\n"; printf("%-1s%-12s%-8s%-4s%-4s%-10s%-33s\n", "H", $outfile, "20101221", "1230", $num, $., " "); for (1 .. $recLines) { last if eof DATA; last if ! defined (my $line = <DATA>); print $line; } printf("%-1s%-12s%-8s%-4s%-4s%-10s%-33s\n", "T", $outfile, "20101221", "1230", $num, $., " "); print "<--- Close file $outfile\n"; ++$num; } __DATA__ 1234567|HP|GASS 1234567|TP|GASS 3456789|YU|GASS 3245678|TY|GASS

Prints:

---> Create file Int_1.txt HInt_1.txt 2010122112301 0 + 1234567|HP|GASS 1234567|TP|GASS TInt_1.txt 2010122112301 2 + <--- Close file Int_1.txt ---> Create file Int_2.txt HInt_2.txt 2010122112302 2 + 3456789|YU|GASS 3245678|TY|GASS TInt_2.txt 2010122112302 4 + <--- Close file Int_2.txt

Note that the sample doesn't include the file creation code, but what you had was ok. However you should always use the three parameter version of open and you should use lexical file handles. Your open should look like:

# input handle: open my $inFile, '<' $source or die "Can't open '$source' $!"; # output handle: open my $outFile, '>' $target or die "Can't open '$target' $!";
True laziness is hard work

In reply to Re: split a file by GrandFather
in thread split a file by chinni2010

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.