http://www.perlmonks.org?node_id=950295


in reply to Re^4: Subsetting text files containing e-mails
in thread Subsetting text files containing e-mails

You don't need more than one pass through the source file. Just create the output files as you need them. In sketch you'd have something like:

use strict; use warnings; my $emailNum; my $outFile; $/ = ''; # Set readline to "Paragraph mode" while (<DATA>) { if (!$emailNum || /^From:/im) { close $outFile if $outFile; my $fname = sprintf "mails_%06d.txt", ++$emailNum; open $outFile, '>', $fname or die "Can't create $fname: $!\n"; } print $outFile $_; }
True laziness is hard work