(updated opening sentence to make better sense; also removed an unnecessary array from the script)#!/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.
In reply to Re: Regexp and reading a file n-lines at time
by graff
in thread Regexp and reading a file n-lines at time
by epimenidecretese
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |