As others have pointed out, part of the problem is having  while (<FH>) in two different places -- this makes it a lot harder to control what's going on.

Even though the data is not XML, it appears to be good enough as SGML. There is SGML::Parser, if you feel so inclined, but with stuff like the example you showed, I tend to be more "trusting" of the data (especially if I have already validated the data separately, e.g. using James Clark's SP package), and I would use some simple perl-isms to chunk my way through it (after I have used a separate, simple perl script to scan the data and make sure my simplifying assumptions are correct). For example:

if ( open( FH, <data.sgl") { local $/ = "</ref>\n"; # make close-tag the input rec. separator while (<FH>) # now read a full "ref" element into $_ { next unless ( m{<aulist>.*?$search.*?</aulist>}is ); # now split the ref element up and print it # <strike>(I'll leave that as an exercise...)</strike> # 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"; }
That assumes that the search term only applies to the contents of the "author list" sub-element within the ref, but it should be easy to see how it would extend to other sub-elements like title, etc.

I'm not sure how much you should worry about checking the content of $search before passing it into a regex match... using the "m{...$search...}" type of usage shouldn't risk any real trouble.


In reply to Re: Searching data file by graff
in thread Searching data file by parisa

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.