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.
In reply to 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
in thread 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 rockstar99
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |