Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

text files

by polr (Initiate)
on Dec 12, 2001 at 19:41 UTC ( [id://131260]=perlquestion: print w/replies, xml ) Need Help??

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

I am reading in a file like
************************ <word> word <word> word <word> word word word <word> word <word> word word <word> word word <word> word word **************************
I want to ammend the first line, only if the second line contains a specific word. Its OK till I want to go back a line. If usin while (<>) I just fall throught the file line by line, but I want to read a line then jump back to the line before can anyone help. The pattern of the file within the ***s may be repeated several hundred times

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: text files
by bmccoy (Beadle) on Dec 13, 2001 at 00:40 UTC
    If your file is small, you can read the entire file into an array, then just go back and forth between array elements. Once you've done your modifications, just write everything back out to disk.

    -- Brett

    Originally posted as a Categorized Answer.

Re: text files
by ariels (Curate) on Dec 13, 2001 at 13:07 UTC
    If your file consists of blocks separated by (say) 60 *'s, you could do this:
    $/ = '*' x 60 . "\n"; while (my $block = <>) { # Do something to $block... print $block; }
    On every iteration, $block will consist of all lines until the next line "******...\n" (including that line).

    Just remember, every block contains multiple lines! This is important if using s/.../.../ in your processing.

    See perlvar for more information about the wondrous $/ variable.

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-16 22:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found