Dr Manhattan has asked for the wisdom of the Perl Monks concerning the following question:
Hi all
I am trying to split a text file so that it prints the first 1000 lines to 1.txt, the 2nd 1000 lines to 2.txt etc etc. I tried this and it seems to be working fine
my $length = $#array; my $fileNR = 1; for (my $x = 0; $x < $length; $x++) { open ($fileNR, ">$fileNR.txt") or die "can't open"; print $fileNR "$array[$x]\n"; if ($x % 1000 == 999) { close ($fileNR); $fileNR++; } }
Except that it doesn't print line 1 - 1000 to a file(as it should do), it just prints every 1000th line in a file, and then it goes on to the next file. Any idea why this happens?
Any help would be appreciated
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Splitting a text file
by jethro (Monsignor) on Mar 19, 2013 at 10:41 UTC | |
|
Re: Splitting a text file
by Ratazong (Monsignor) on Mar 19, 2013 at 10:51 UTC | |
by Dr Manhattan (Beadle) on Mar 20, 2013 at 05:05 UTC | |
by Ratazong (Monsignor) on Mar 20, 2013 at 08:06 UTC | |
|
Re: Splitting a text file
by daxim (Curate) on Mar 19, 2013 at 10:54 UTC | |
by farang (Chaplain) on Mar 20, 2013 at 17:03 UTC |