Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

text files

by polr (Initiate)
on Dec 12, 2001 at 19:41 UTC ( #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.

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?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2023-10-04 11:06 GMT
Find Nodes?
    Voting Booth?

    No recent polls found