in reply to Regexp and reading a file n-lines at time

If you are confident that all the blank lines are really blank (no spaces or tabs), and if the organization of each recipe really has the same sequence of elements every time, you can read in "paragraph mode" (see the description of the INPUT_RECORD_SEPARATOR variable $/ in perlvar):
#!/usr/bin/perl use strict; use warnings; $/ = ""; # empty string sets "paragraph mode": reads up-to/including +blanks my @parts; my $xml_format = "<recipe>\n<title>%s</title>\n". " <abstract>\n%s\n </abstract>\n". " <ingredients>\n%s\n </ingredients>\n". " <procedure>\n%s\n </procedure>\n</recipe>\n"; print "<cookbook>\n"; while (<DATA>) { s/\n+$//; # trim off trailing line-breaks if ( /^\s*\d/ ) { # record begins with a number: start of new rec +ipe if ( @parts ) { # print previous recipe if there was one printf( $xml_format, @parts ); @parts = (); } push @parts, $_; } elsif ( @parts == 4 ) { # we have title, abstract, ingredients and + some procedure $parts[3] .= "\n\n$_"; # so just append this paragraph to pro +cedure } else { # this is either the abstract, ingredients or start of proc +edure push @parts, $_; } } printf( $xml_format, @parts ) if ( @parts ); print "</cookbook>\n"; __DATA__ 1.TITLE OF FIRST RECIPE abstract Ingredient 1. Ingredient 2. Ingredient n... Procedure... 2.TITLE OF SECOND RECIPE second abstract rum cola ice Just mix it all in a glass, drink it and be happy.
(updated opening sentence to make better sense; also removed an unnecessary array from the script)