You seem to be dealing with record oriented data where records are separated by empty lines. Often it is convenient to read a record as a time. Consider:
use strict; use warnings; my $data = <<DATA; this is test only 1 Baki: 9.70 this is test only 2 Baki : 9.30 this is test only 3 Baki : 9.00 DATA my $lastRecord; open my $in, '<', \$data; local $/ = "\n\n"; # Read a record at a time while (defined (my $record = <$in>)) { chomp $record; next if $record =~ /$(?:Baki)/; $lastRecord = $record; } print $lastRecord;
Prints:
this is test only 3 Baki : 9.00
Note the use of three parameter open and a lexical file handle. Usually there would be an or die ...; for the open as well, but in this case (using a string as a file) there is no need.
In reply to Re: how to find last word match ?
by GrandFather
in thread how to find last word match ?
by bh_perl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |