if ( open( FH, \n"; # make close-tag the input rec. separator while () # now read a full "ref" element into $_ { next unless ( m{.*?$search.*?}is ); # now split the ref element up and print it # (I'll leave that as an exercise...) # Since you have a finite number of things you want # to put into your output listing, it'll be easiest # just to look for those things: for my $fld ( qw/author year source keys title/ ) { @{$data{$fld}} = m{<$fld>([^<]+)}ig; } print_record( \%data ); # all hash elements are array refs # (some may be empty/undef, some may have 1 item) } close FH; } else { die "Unable to read data.sgl\n"; }