while() { push @stock,$_; } # Would be better as... @stock = ; #### while(@stock) { foreach $line (@stock) { #### while (my $line = ) { # do stuff } #### open $item[2], ">>some_filename"; put_items_into_it; close $item[2] #### my $cat = undef; while (my $line = ) { @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;