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. > > Thanks > Brad > > > Code: > > #! /usr/bin/perl > > my $input = 'test12.txt'; > my $end= '.txt'; > my $fileno=1; > my $lineno=1; > open BIG, "test12.txt", or die "can't $!"; > > while (<BIG>) { > > print "$lineno"; > $newfile = "$fileno$end"; > $fileno++; > open NEW ,"> $newfile"; > > while ($lineno < 71){ > $lineno++; > print NEW $_; > }}
There are number of things not quite right: Out loop read and inner write loop, but only doing one read. Should close after writing the file. Here is a sample that I put together. Also should use warnings and strict. Also no error checks on the open. #! /usr/bin/perl my $input = 'test12.txt'; my $end= '.txt'; my $fileno=1; my $lineno=1; #open BIG, "test12.txt", or die "can't $!"; my $newfile; my $MyWrkLoc = q[output location for the files]; opensub(); while (<DATA>) { if ( $lineno >= 71 ) { close NEW; opensub(); } print NEW $_; $lineno++; } sub opensub { $newfile = sprintf "%09d%s", $fileno++, $end; open NEW ,q[>] . $MyWrkLoc . $newfile; $lineno = 1; } Should get you farther If you have any problems or questions, please let me know. Thanks. Wags ;) David R Wagner Senior Programmer Analyst FedEx Freight 1.408.323.4225x2224 TEL 1.408.323.4449 FAX http://fedex.com/us ********************************************************************** This message contains information that is confidential and proprietary to FedEx Freight or its affiliates. It is intended only for the recipient named and for the express purpose(s) described therein. Any other use is prohibited. ********************************************************************** -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/