Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

how to find last word match ?

by bh_perl (Monk)
on Nov 24, 2010 at 06:56 UTC ( [id://873367]=perlquestion: print w/replies, xml ) Need Help??

bh_perl has asked for the wisdom of the Perl Monks concerning the following question:


hi

This is my input file
this is test only 1 Baki: 9.70 this is test only 2 Baki : 9.30 this is test only 3 Baki : 9.00
This is my code
open(CREDIT, "c/Log.txt"); while (my $credit = <CREDIT>) { chomp($credit); print ("$credit\n"); } close(CREDIT);

The problem is how could i read the input file and get the last word match with "Baki". Based on the sample, the result must be "Baki : 9.00"

Thank you,

baharin

Replies are listed 'Best First'.
Re: how to find last word match ?
by GrandFather (Saint) on Nov 24, 2010 at 07:44 UTC

    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.

    True laziness is hard work
Re: how to find last word match ?
by Ratazong (Monsignor) on Nov 24, 2010 at 07:34 UTC
    A typical algorithm to find the last entry in a data-structure that you can only read sequentially from the beginning is the following:
    • create a variable for your result (e.g. my $last_baki = "no baki there";)
    • use a loop to read the whole data-structure element-by-element (in your case: line-by-line)
      • if the element matches your requirements (in your case: contains the word baki), overwrite the result-variable with this element
    • when you have finished your loop, the result-variable will contain the last matching entry
    HTH, Rata
Re: how to find last word match ?
by Anonymous Monk on Nov 24, 2010 at 07:15 UTC
    The problem is how could i read the input file and get the last word match with "Baki".

    What do you think?

      <hi>
      This is my opinion by using shell script
      system ("cat file.txt |grep "Baki" |tail -1");

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://873367]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2024-04-25 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found