seaver has asked for the wisdom of the Perl Monks concerning the following question:
I am simply opening one file, reading each line, and according to the nature of the line, appending it to one of 2 or 3 files, thus 'sorting' the file into seperate files.
I can either, go through the original FILE 3 times, and each time, only write the lines of one particular nature to a file.
OR, I can do through FILE once, but at each line, check the nature, and append to the appropiate file:foreach my $n (@natures){ open (OUT, "> $n.txt") or die "blah blah $! \n" while(<FILE>){ print OUT $_ if $_ =~ /$n/; } }
I was wondering, seeing FILE has thousands of lines, whether, reiterating through those thousands would be any quicker than opening and closing several files once for every line...while(<FILE>){ foreach my $n (@natures){ if($_ =~ /$n/){ open (OUT, ">> $n.txt") or die "blah blah $! \n" print OUT $n; close OUT; } } }
Cheers
S
|
|---|