[tpm] how to delete array entries en-passant
stuart at morungos.com
Wed Jun 30 09:45:43 PDT 2010
You could take the alternative route of turning the string into a file handle, using open on a reference to the string (an example is shown in perldoc -f open), and then having a single regular while loop over lines read from a file handle (whether from string or file), chomping and processing. This would likely be the most memory efficient, as you'd never need a second copy of the input in memory. Useful if you could be dealing with very large files.
I think this may be Perl 5.10. Perl 5.8 might use a separate module, IO::String, for the same purpose, if I remember right.
Just a thought - but I do love the way Perl allows you to work with a file handle backed by a string so easily.
All the best
On 2010-06-30, at 9:35 AM, Fulko Hew wrote:
> On Wed, Jun 30, 2010 at 12:31 PM, <arocker at vex.net> wrote:
> > OK, let me give you the specific example...
> > The array contains all of the lines of a file...
> > The foreach is used to loop over then, and 'take action' upon them.
> > But when I'm done I want all the empty lines stripped from the array
> > ('just
> > because!')
> Does the process require the presence of the entire file in memory before
> the loop, and the shuffled-down file afterwards? Do you output the
> remaining lines?
> No, but I need to output the 'remaining' lines.
> Also I forgot to mention, the reason the original data was in an array, was because
> it came from one of two sources... slurping a file, or a 'received' string.
> So I thought I'd first normalize either dataset into the array I would then traverse.
> toronto-pm mailing list
> toronto-pm at pm.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the toronto-pm