in reply to Find the first occurance and if not found print the first line
Here's an approach which sub-divides the problem into parsing a Query and then processing it:
Now you can examine all of the gi lines in a query: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 }
This way it should be easier to implement whatever processing logic you need.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 ... } } }
|
|---|