Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: inline file modification

by joeface (Pilgrim)
on Sep 07, 2005 at 17:50 UTC ( #489953=note: print w/replies, xml ) Need Help??

in reply to inline file modification

Look at the Tie::File module. It allows you to edit a file in-place as if it were an array.

This example replaces any line containing $idtag with a line containing $replacement.

use Tie::File; my $file = 'filename'; my @tied_file; my $idtag = 'idTag'; my $replacement = 'replacement line'; # Create the array @tied_file from $file tie(@tied_file, 'Tie::File', $file); # Preserve the index so we can work directly on lines of @tied_file foreach my $i (0 .. $#tied_file) { if ($tied_file[$i] =~ /$idtag/) { # Do something to this line of @tied_file $tied_file[$i] = $replacement; } } untie(@tied_file);

Replies are listed 'Best First'.
Re^2: inline file modification
by Woodchuck (Initiate) on Sep 07, 2005 at 18:17 UTC
    Thank you, this is just the bit I needed. I suggest you put this in the snippets barn,as it might prove to be useful to a lot of people.
    keyword-> read write file
    Thanks again!


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://489953]
[marto]: perldebug, never heard of the editor
[ovedpo15]: perldebug isn't any help because I need a GUI debbuger to see the value of my elements in each step. any suggestions?

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2018-05-25 18:36 GMT
Find Nodes?
    Voting Booth?