mndoci has asked for the wisdom of the Perl Monks concerning the following question:
I have a file which has the following format
245
record #1
record #2
.
.
record #245
====
165
record #1
record #2
.
.
record #165
====
As you can make out, the first line is the number or records in that set (till "===="). There are 20 such records. Instead of regexing, I used the following
#initialize some variables my $records; # No. of records my $last; # The last line my $ini = 1; # set initial line my $count = 1; # counter for (my $count = 1; $count <= 20; $count++){ open (OLD, "< $old_file.txt") || die $!; # open file # Now we need to split up the file into 20 files open (NEW, "> $old_file$count.txt") || die $!; #open outputfile while (<OLD>){ if($. == $ini){ chomp; $records = $_; } } close OLD; $last = $ini + $records + 1; open (OLD, "<$old_file.txt") || die $!; # I am more comfortable with r +e-opening the file like this while (<OLD>){ next unless $. >= $ini; print NEW $_; last if $. >= $last; } close OLD; close NEW; $ini = $last + 1; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(jeffa) Re: To regex or not to regex
by jeffa (Bishop) on Mar 11, 2002 at 05:35 UTC | |
by abstracts (Hermit) on Mar 11, 2002 at 06:07 UTC | |
by mndoci (Scribe) on Mar 11, 2002 at 06:11 UTC | |
|
Re: To regex or not to regex
by Anonymous Monk on Mar 11, 2002 at 06:00 UTC | |
by abstracts (Hermit) on Mar 11, 2002 at 06:46 UTC | |
by mndoci (Scribe) on Mar 11, 2002 at 07:11 UTC | |
by mndoci (Scribe) on Mar 11, 2002 at 07:14 UTC | |
|
Re: To regex or not to regex
by jryan (Vicar) on Mar 11, 2002 at 18:32 UTC |