in reply to reading file into an array

You know, there's another method that I don't see having been mentioned in this thread yet.

You could use the Tie::File module.

Some might say it's overkill if all you want is to slurp a file into an array. But the module is great if you want to access the file's lines as though they are in an array, but without the disadvantage of slurping the whole thing into memory at the same time.

Tie::File also allows for in-place editing; if you alter the contents of one of a a tied array's elements, that change is written to the file.

use Tie::File; tie my @array, 'Tie::File', Original or die "Error: $!"; # Do your stuff with @array now.

Enjoy!


Dave

Replies are listed 'Best First'.
Re: Re: reading file into an array
by halley (Prior) on Jun 01, 2004 at 17:26 UTC
    While Tie::File is really cool, and all, I never recommend it to newcomers to Perl. I much rather the newcomer get a good understanding of the "primordial" features of the language, than to gloss over them with magic modules that do the same thing at arm's length.

    Once someone has gotten so familiar with the primitives, that such a loop is seen as drudgery, then a module like Tie::File is an interesting diversion or convenience.

    Or suggest it once someone sees the limitations or complications in the classic iteration. For example, to insert two lines before the last forty lines, it would take some marginally creative queueing to accomplish without Tie::File.

    --
    [ e d @ h a l l e y . c c ]