Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: how to read a particular paragraph from middle of a file line by line and enter each string in the line as an element of an array

by TJPride (Pilgrim)
on Nov 28, 2011 at 10:52 UTC ( #940343=note: print w/ replies, xml ) Need Help??


in reply to how to read a particular paragraph from middle of a file line by line and enter each string in the line as an element of an array

Why people are upvoting vinian's obviously broken and insufficient solution, I don't know.

use strict; use warnings; my ($file, $handle, @arr); open($handle, 'test.txt'); BREAKPOINT: while (<$handle>) { if (m/Here starts the second paragraph/) { chomp; push @arr, $_ for split /\s+/, $_; while (<$handle>) { chomp; last BREAKPOINT if !$_; push @arr, $_ for split /\s+/, $_; } } } print join "\n", @arr;

Where test.txt contains:

This is the start of the first paragraph. This is the second line of this first paragraph. And this is the last line of it. Here starts the second paragraph. This is the second line of this second paragraph. And this is the last line of it. Here starts the third paragraph. This is the second line of this third paragraph. And this is the last line of it. Here starts the fourth paragraph. This is the second line of this fourth paragraph. And this is the last line of it.

And output is:

Here starts the second paragraph. This is the second line of this second paragraph. And this is the last line of it.


Comment on Re: how to read a particular paragraph from middle of a file line by line and enter each string in the line as an element of an array
Select or Download Code
Re^2: how to read a particular paragraph from middle of a file line by line and enter each string in the line as an element of an array
by Anonymous Monk on Nov 30, 2011 at 08:39 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-08-02 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (53 votes), past polls