If you indented your code a bit, it would be a lot easier to read :-)
Looks like you're making it all a bit more complex than it needs to be. Here's a program that I wrote to do the same thing - perhaps it will be useful to you.
#!/usr/bin/perl use strict; use warnings; use POSIX 'ceil'; die "Usage: splitfile num_of_files initial_file\n" unless @ARGV >= 2; my ($count, $infile) = @ARGV; open INFILE, '<', $infile or die $!; my @lines = <INFILE>; my $lines_per_file = ceil(@lines / $count); my $i = 1; open OUTFILE, '>', "$infile.$i" or die $!; my $lines_left = $lines_per_file; foreach (@lines) { unless ($lines_left) { close OUTFILE; $i++; open OUTFILE, '>', "$infile.$i" or die $!; $lines_left = $lines_per_file; } print OUTFILE; $lines_left--; }
"The first rule of Perl club is you do not talk about
Perl club."
-- Chip Salzenberg
In reply to Re: Problem with multiple loops
by davorg
in thread Problem with multiple loops
by chinamox
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |