[Gllug] Perl Problem

Dave Cross dave at dave.org.uk
Thu Feb 1 07:21:43 UTC 2007


Brad Cahoon wrote:

> Hi Perl Masters
> 
> I have a problem with a script which is suposed to open a huge text
> file and take 70 lines, create a file, then take the next 70 lines
> create a file and so on until it has parsed the whole file. My code
> just doesn't work and my brain cannot figure out while{while{}} loops
> Help please.

Why do people always over complicate these things?

#!/usr/bin/perl

use strict;
use warnings;

my $input = 'test12.txt';
my $end = '.txt';
my $fileno = 1;

open BIG, '<', $input, or die "can't $!";
open SMALL, '>', "$fileno$end" or die "Can't open output: $!";

while (<BIG>) {

   print SMALL;

   unless ($. % 70) {
     close SMALL;
     ++$fileno;
     open SMALL, '>', "$fileno$end" or die "Can't open output: $!";
   }
}

-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list