Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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 vinian (Beadle)
on Nov 28, 2011 at 08:23 UTC ( [id://940322]=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

open my $fh, '<', $file or die "failed open file: $!"; my $arr = []; my $beginParagraph = 'begin'; # mark start the particular paragraph my $endParagraph = 'end'; # mark end the particular paragraph while ( <$fh> ){ if ( /$beginParagraph/ .. /$endParagraph/ ) { push @{ $arr->[$.] }, split /\./; # i assume string end '.' } }

update

use strict; use warnings; use Data::Dumper; my $arr; my $beginParagraph = 'Here starts the second paragraph'; # mark star +t the particular paragraph my $endParagraph = 'And this is the second line of it'; # mark en +d the particular paragraph while ( <DATA> ){ if ( /$beginParagraph/ .. /$endParagraph/ ) { chomp; push @$arr, split /\s+/; } } print Dumper($arr); __DATA__ 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. And this is the second string. This is the second line of this second paragraph. And this is the second line of it. Here starts the third paragraph. This is the second line of this
Output:
$VAR1 = [ 'Here', 'starts', 'the', 'second', 'paragraph.', 'And', 'this', 'is', 'the', 'second', 'string.', 'This', 'is', 'the', 'second', 'line', 'of', 'this', 'second', 'paragraph.', 'And', 'this', 'is', 'the', 'second', '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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-20 03:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found