in reply to split file in N part
Not bad. But I think you have a bug. Probably just a typo. I believe
should bemy $nbLinesPerFile=$totalCount;
Right?my $nbLinesPerFile=int( $totalCount / $nbFiles );
Aside from that, there's a number of places I think your code could be improved. In particular, I'd try to make more use of existing modules.
One useful thing is to use Carp when issuing error messages, since it tends to give better context.
In sub countLines, you should localize $_. Even better would be to use a lexical variable for the purpose. If it were me, I'd probably use something like Tie::File to get the number of lines:
Such a tied array could also be used in the other sub, for iterating over the lines of input.use Tie::File; use Carp; sub countLines { my $filename = shift; tie my @array, 'Tie::File', $filename or croak("failed to tie '$filename' - $!"); scalar @array }
You also need to do error checking on all your open calls — and be sure to include the "reason" ($!) in the error message!
Ultimately, you could just use File::Split:
:-)use File::Split; File::Split ->new({keepSource=>'1'}) ->split_file({parts=>$nbFiles},$newfile);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: split file in N part
by BrowserUk (Patriarch) on Mar 10, 2008 at 15:27 UTC | |
by jdporter (Paladin) on Mar 10, 2008 at 18:06 UTC | |
by BrowserUk (Patriarch) on Mar 10, 2008 at 18:24 UTC | |
|
Re^2: split file in N part
by jeepj (Scribe) on Mar 10, 2008 at 14:29 UTC |