Hi Perl-List!

Stuart Clemons wrote:
[...]
> 
> 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.

Here's a simple version that writes matching records to STDOUT...

#!/usr/bin/perl
use warnings;
use strict;

# sample patterns
my $DateField1 = '07/\d+/77';   # July 77
my $Name = "Doe\n";             # Lastname is "Doe"

$/ = "";                        # treats empty line(s) as record terminator
while(<DATA>) {
   if (m{DateField1:\s+$DateField1} ||  
       m{Name:.*$Name}) {
      print;
   }
}

-- 
Kevin Pfeiffer

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to