in reply to Problem with multiple loops

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--; }
--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: Problem with multiple loops
by chinamox (Scribe) on Oct 24, 2006 at 12:57 UTC

    Sorry about the indents I went back in fixed that ASAP

    Thank you very much for your help. As you can see am still very new to this whole Perl thing and I tend to overthink this just a little ;). I believe I am suppose to be learning how to use File::IO for this assignment, and so would like the resist using solutions that are too clever for me to fully understand

    That said, your code is amazingly useful, as in the last five minutes I think I have already seen several problem/imporvements that I should fix or make.

    Thanks again,

    -mox