Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Putting files in an Array

by blue_cowdawg (Monsignor)
on Jul 11, 2012 at 15:20 UTC ( #981157=note: print w/replies, xml ) Need Help??

in reply to Putting files in an Array

Personally for treating a file like an array I am a fan of using Tie::File

From the CPAN site:

# This file documents Tie::File version 0.98 use Tie::File; tie @array, 'Tie::File', filename or die ...; $array[13] = 'blah'; # line 13 of the file is now 'blah' print $array[42]; # display line 42 of the file $n_recs = @array; # how many records are in the file? $#array -= 2; # chop two records off the end for (@array) { s/PERL/Perl/g; # Replace PERL with Perl everywhere i +n the file } # These are just like regular push, pop, unshift, shift, and s +plice # Except that they modify the file in the way you would expect push @array, new recs...; my $r1 = pop @array; unshift @array, new recs...; my $r2 = shift @array; @old_recs = splice @array, 3, 7, new recs...; untie @array; # all finished

Essentially anything you do with an array you can do with a file.

Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://981157]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-05-22 18:04 GMT
Find Nodes?
    Voting Booth?