Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: how to edit a file

by CubicSpline (Friar)
on Dec 11, 2001 at 19:41 UTC ( [id://130949]=note: print w/replies, xml ) Need Help??


in reply to how to edit a file

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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://130949]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-26 01:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found