while(<FILE>) { push @stock,$_; } # Would be better as... @stock = <FILE>;
Why wrap a while around a foreach here?
while(@stock) { foreach $line (@stock) {
A straight foreach will do just as well. Of course in that case there is no need for @stock at all...
while (my $line = <FILE>) { # do stuff }
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...
open $item[2], ">>some_filename"; put_items_into_it; close $item[2]
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.
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;
delete_that_line_from_stock;
Is not neccessary, since there is no @stock anymore.

In reply to Re: Re: About Filehandles by Sifmole
in thread About Filehandles by Chady

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.