On Thu, 2002-02-14 at 10:46, [EMAIL PROTECTED] wrote:
> On Thu, Feb 14, 2002 at 10:40:20AM -0500, Chas Owens wrote:
> > On Thu, 2002-02-14 at 10:19, Martin A. Hansen wrote:
> > >
> > > hi
> > >
> > > i have inherited this piece of code and i have been wondering hard about how i
>can print the different @records returned from the subroutine? im not even sure whats
>on my hand here. is this a hash of arrays or a hash of array references? and how do i
>unwind those?
> > >
> > >
> > >
> > > martin, beginning perl.
> > >
> > >
> > >
> > >
> > > my @records = &parse_pubmed_fcgi( [ qw(UI AU TI TA VI IP PG DA) ] );
> > >
> > >
> > > ###################### subroutines ####################
> > >
> > >
> > > sub parse_pubmed_fcgi {
> > >
> > > my ( $keys ) = @_;
> > >
> > > my ( @records, $record, $record_id, $line, $key );
> > >
> > > my %wants = map { $_ => 1 } @{ $keys };
> > >
> > > while ( defined ($line = <>) ) {
> > > chop $line;
> > >
> > > if ( $line =~ /^(UI)\s*-/ ) {
> > > $key = $1;
> > >
> > > if ( $record ) {
> > > push @records, $record;
> > > undef $record;
> > > }
> > > }
> > >
> > > if ( $line =~ /^([A-Z]+)\s*-\s*(.+)/ ) {
> > > $key = $1;
> > > push @{ $record->{ $key } }, $2 if $wants{ $key };
> > > }
> > > elsif ( $line =~ /^\s+(\S.*)/ ) {
> > > $record->{ $key }->[-1] .= " $1" if $wants{ $key };
> > > }
> > > }
> > >
> > > push @records, $record if $record;
> > >
> > > if ( wantarray ) {
> > > return @records;
> > > } else {
> > > return \@records;
> > > }
> > >
> > > }
> > >
> >
> > Data::Dumper is your friend. If you have a data structure and you don't
> > know what is in it just say:
> >
> > print Dumper(\@Records);
> >
> > and bingo the data structure is printed out to the screen.
> >
> > --
> > Today is Setting Orange the 45th day of Chaos in the YOLD 3168
> > Kallisti!
> >
> > Missle Address: 33:48:3.521N 84:23:34.786W
> >
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
>
> ok, if Data::Dumper is my friend, how can i print all record fields of type AU only?
>
> martin
> --
Data::Dumper isn't that good of a friend (he may loan you his truck, but
he won't help you move).
#you can print all subsubrecords of type subrecord type KEYONE by
uttering
#foreach my $record (@$VAR1) {
# foreach my $subrecord (@{$record->{KEYONE}}) {
# print $subrecord
# }
#}
#see below for my reasoning
sub parse_pubmed_fcgi {
my ( $keys ) = @_;
my ( @records, $record, $record_id, $line, $key );
my %wants = map { $_ => 1 } @{ $keys };
while ( defined ($line = <>) ) {
chop $line;
#UI appears to be the start of record signal
if ( $line =~ /^(UI)\s*-/ ) {
$key = $1;
#I suspect this because the old record (if there
is one) #gets pushed onto the return array here
if ( $record ) {
push @records, $record;
undef $record;
}
}
#It also appears to be the first subrecord since
#this isn't an elsif. Anywho, any line that starts with
A
#through Z is a subrecord. The first word is the key
and the
#data follows on the dash
if ( $line =~ /^([A-Z]+)\s*-\s*(.+)/ ) {
$key = $1;
#apparently we can specify what keys to hover up
from
#the file. If we want this subrecord type then
we push
#the subrecord onto an array contained in the
hash
#based on the subrecord type
push @{ $record->{ $key } }, $2 if $wants{ $key
};
}
elsif ( $line =~ /^\s+(\S.*)/ ) {
#ooo, looky: we can have line continuations,
this
#data is just part of the last subrecord
$record->{ $key }->[-1] .= " $1" if $wants{ $key
};
}
}
#this is more evidence that ^UI starts a record since the file
ends
#before we get another one
push @records, $record if $record;
if ( wantarray ) {
#if the function is in list context return the array
return @records;
} else {
#else retrun a reference to the array (don't worry
#another array will be created by a second call
#so there are no messy pointer issues like in C
return \@records;
}
}
#so we have an array (@records) of hashes ($record, repeatedly)
#whose keys match ^A-Z+ and whose values are arrays of file entries
#that matched the ^A-Z+. The structure should look somethign like this:
my $VAR1 = [
{
UI => [
"howdy this is the start of the first record"
],
KEYONE => [
"data one (this was on the second line with a
tab)",
"data two I am a record as well",
"data three, Hi there!"
],
KEYTWO => [
"more data",
"even more data"
],
KEYTHREE => [
"enough data aleady"
]
},
{
UI => [
"howdy this is the start of the second record"
],
KEYONE => [
"data one (this was on the second line with a
tab)",
"data two I am a record as well",
"data three, Hi there!"
],
KEYTWO => [
"more data",
"even more data"
],
KEYTHREE => [
"enough data aleady"
]
}
];
--
Today is Setting Orange the 45th day of Chaos in the YOLD 3168
Frink!
Missle Address: 33:48:3.521N 84:23:34.786W
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]