polr has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: how to edit a file
by CubicSpline (Friar) on Dec 11, 2001 at 19:41 UTC
    Your question could use more details. I'll assume you're reading a list from a file, editing it, and writing it back to the same file.

    open FILE, "myfile.txt" or die "Couldn't open for read!"; # stick all lines of file into an array of strings @lines = <FILE>; close FILE; open FILE, ">myfile.txt" or die "Couldn't open for write!"; foreach $line (@lines) { # create a list of words in each line @words = split ' ', $line; # editing code goes here if( $words[1] eq "posp" ) { $words[0] = "edited"; } # editing is done, now write the line to the file print FILE $_." " foreach( @words ); print FILE "\n"; } close FILE;