Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to split a file by paragraph? instead of by lines

Status
Not open for further replies.

orangegal

Technical User
Oct 12, 2005
15
US
thanks for your help
 
define a paragraph, two line feeds in succession?
Code:
$/=undef #this you'll need to check
open FILE, "<file.txt";
$file=<FILE>;
@paras=split /\n\n/, $file;
foreach(@paras) {
   print "$_";
   print "="x40;
}

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Code:
open FILE, "file.txt"; 
@paras=<FILE>;

each paragraph in an element of the array.
 
Code:
open FILE, "file.txt";
@paras=<FILE>;
each paragraph in an element of the array.

that will put each line of the file into each element of the array. If each line happens to be each paragraph then it will work, but that seems unlikely.
 
Assuming that the paragraphs are delimited as Paul suggested, you could use code similar to zephan's by changing the input record separator.

Code:
{local $/ = "\n\n";
 map {chomp; print "\|$_\|\n"} <DATA>;}

__DATA__
Here is
one paragraph
of text.

This is a
second paragraph
of text.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top