in reply to Re: About Filehandles
in thread About Filehandles
Why wrap a while around a foreach here?while(<FILE>) { push @stock,$_; } # Would be better as... @stock = <FILE>;
A straight foreach will do just as well. Of course in that case there is no need for @stock at all...while(@stock) { foreach $line (@stock) {
Additionally this will not be memory intensive because it will only store one line in memory at a time. If you are attempting to keep only one filehandle open at a time via...while (my $line = <FILE>) { # do stuff }
You might be better off using checking if the next item to be written out goes into the same FILE that is already opened. No reason to close it and open it again immediately.open $item[2], ">>some_filename"; put_items_into_it; close $item[2]
my $cat = undef; while (my $line = <FILE>) { @items = split('|', $line); if ($cat ne $items[2]) { close OUTFILE if (defined $cat); open OUTFILE, ">>some_filename"; $cat = $items[2]; } print OUTFILE ## Whatever ###; } close OUTFILE;
Is not neccessary, since there is no @stock anymore.delete_that_line_from_stock;
|
|---|