As I understand what you are doing, the problem with your approach is that in the third Query there are three gi lines but the ref is in the second gi line. Your logic to determine if a sequence of gi lines has a ref in it is faulty because it will only check the first gi line (once you detect a gi line you gobble up the rest of them.)

Here's an approach which sub-divides the problem into parsing a Query and then processing it:

use Data::Dumper; my $query; while (<IN>) { if (s/^Query=\s*//) { my $save = $_; process($query); } $query = { query_line => $save }; } elsif (m/(\d+)\s*letters/) { $query->{letters} = $1; } elsif (m/^gi/) { push(@{$query->{gi_lines}}, $_); } } process($query); sub process { print Dumper($_[0]); # for now }
Now you can examine all of the gi lines in a query:
sub process { my $query = shift; return unless $query; # first time $query will be undef ... # iterate through all the gi lines for my $gi_line (@{$query->{gi_lines}}) { if ($gi_line =~ m{\|ref\|}) { ... ref found ... } } }
This way it should be easier to implement whatever processing logic you need.

In reply to Re: Find the first occurance and if not found print the first line by pc88mxer
in thread Find the first occurance and if not found print the first line by sm2004

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.