Parsing structured files in Perl

Hi,

looking for a piece of code to get the values from a structured text file like this:

###############################################################################
###############################################################################
#
# PERSONAL DATA DEFINITIONS
#
###############################################################################
###############################################################################


define person{
        first_name      John
        last_name       Peterson
        nickname        Peter
        address         1024 Maple street
        birthday        November 11th, 1970
        }
        
define office{
        use                home office
        office_name        main
        office_description home post office
        }


define person{
        first name      Jamie
        last name       Bluff
        nickname        Bluff
        address         134 Downing street
        birthday        October 15th, 1974
        }


define person{
        first name      Melanie
        last name       Houston
        nickname        Mell
        address         111 Boston street
        birthday        January 5th, 1969
        }
        
define office{
        use                foreign office
        office_name        main
        office_description foreign post office
        }        


define person{
        first name      Blake
        last name       Dawson
        nickname        Blakey
        address         512 Kendrick street
        birthday        February 6th, 1970
        }

Need to get only values from "define person" parts of the file. The rest needs to be
ignored.

Thanks and regards,

Ed

Try this,

perl -nle 'print if /define person\{/.. /\}/' filename

To have "paragraphs":

perl -0777 -ne  'print "$_\n\n" for /(define person\{.*?\})/gs' INPUTFILE    

yazu, thank you very much for this, it works from the command line. But I'd like to use it from within
perl script. Is there a way to use the command above in the perl script?

A sketch.

undef $/;
$_=<>;
print "$_\n\n" for /(define person\{.*?\})/gs;

Thanks a lot. It works perfectly. Another question: is it possible each time when the structure
"define person {...}" is found in the file, to put it in an array which would then contain a single set of
data about one person (and not to print it to STDOUT)?

undef $/;
$_=<>;
my @persons = /(define person\{.*?\})/gs;

How about this,

#!/usr/bin/perl

my %persons;

while (<DATA>) {
if (/define person\{/ .. /\}/) {
        if (/define/) {
                $var="per".++$i;
                next;
        }
        @ar=split;
        $detail=$ar[0];
        delete $ar[0];
        $persons{$var}->{$detail}="@ar";
}
}
for ($j=1;$j<=$i;$j++) {
print "Person :-",$j,"\n";
print $persons{'per'.$j}->{'first_name'},"\n";
print $persons{'per'.$j}->{'last_name'},"\n";
print $persons{'per'.$j}->{'nickname'},"\n";
print $persons{'per'.$j}->{'address'},"\n";
print $persons{'per'.$j}->{'birthday'},"\n";
}


__DATA__
###############################################################################
###############################################################################
#
# PERSONAL DATA DEFINITIONS
#
###############################################################################
###############################################################################


define person{
        first_name      John
        last_name       Peterson
        nickname        Peter
        address         1024 Maple street
        birthday        November 11th, 1970
        }

define office{
        use                home office
        office_name        main
        office_description home post office
        }


define person{
        first_name      Jamie
        last_name       Bluff
        nickname        Bluff
        address         134 Downing street
        birthday        October 15th, 1974
        }


define person{
        first_name      Melanie
        last_name       Houston
        nickname        Mell
        address         111 Boston street
        birthday        January 5th, 1969
        }

define office{
        use                foreign office
        office_name        main
        office_description foreign post office
        }


define person{
        first_name      Blake
        last_name       Dawson
        nickname        Blakey
        address         512 Kendrick street
        birthday        February 6th, 1970
        }

Yazu, thank you very much for your time and help.