Stuart Clemons wrote: > > Hi all: > > This newbie is back looking for some hints on how to handle this problem > in processing a file that has multiline records that look something like > this: > > Name: Joe Blow > DataField1: xxxxx > DateField1: 07/07/77 > DataField2: xxxxx > DateField2: 12/07/03 > > Name: Fi Doe > DataField1: xxxxx > DateField1: 08/08/88 > DataField2: xxxxx > DateField2: 12/12/03 > > etc. > > There is an empty line that separates each record. I need to extract the > records that meet certain criteria. I would really like to know how to > do this in Perl. > (For a simple task like this, in the past, I would just import these > records into a database and write a query to extract the records that I > wanted.) > > I've actually thought a lot about the problem, but I haven't done any perl > coding that would allow me to put my woeful perl ineptitude on public > display in this forum. I hope to find the time to start on this problem > in the next couple of days. As I'm certain to run into problems, I will > then share my ineptitude while looking for proper guidance from the valued > learned ones. > > Until then, here's what I was thinking to do. What I thought I would do > is try to read each line of one record into an array or hash, then use a > conditional to determine if that record meets the desired criteria. If it > meets the criteria, write out this record to another file and then read > in the next record. If the record doesn't meet the criteria, read in the > next record. I would keep doing this until EOF. > > I think I can handle the conditional stuff, but what I don't know how to > do is read in each line of the record and stop reading when I hit the > empty line so that I can do the conditional stuff. > > Is this a valid approach to take for this problem? And if so, should I > use an array or hash ? And again, how do I stop reading the input file > when I hit the empty line so that I can do the conditional stuff ? > > Thanks in advance for any help and/or hints. The feedback from my last > question was extremely helpful as I struggle to get Perl to do what I want > it to do. I think I'm making progress, although it doesn't always feel > that way !
When you have an empty line between "records" think "paragraphs" and use perl's built-in paragraph mode for the input record separator. If you are just outputing records that match a certain criteria there is no need to use an array or hash, just print the records that match. open IN, '<', 'original' or die "Cannot open 'original': $!"; open OUT, '>', 'newfile' or die "Cannot open 'newfile': $!"; $/ = ''; # set paragraph mode on input record separator while ( <IN> ) { my ($date) = /^DateField1:\s*(.+)/m; print OUT if $date eq '08/08/88'; } __END__ John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>