Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Finding the last statement in string of atmost similar pattern

by amma (Novice)
on May 06, 2013 at 07:05 UTC ( [id://1032189]=perlquestion: print w/replies, xml ) Need Help??

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

I have following data as a string filehandle and I want to find the last statement of repeated pattern!!

abc(1) = xyz(0); abc(2) = xyz(1); abc(3) = xyz(2); ...............: abc(10) = xyz(9);

I would like to find the abc(10) = xyz(9);

Replies are listed 'Best First'.
Re: Finding the last statement in string of atmost similar pattern
by hdb (Monsignor) on May 06, 2013 at 07:29 UTC

    So you want the last non-empty line in your file? I am using a string instead of your file.

    use strict; use warnings; my $string; # replacement for your file $string .= "abc($_) = xyz(".($_-1).");\n" for 1..10; my $line; for ( split /\n/, $string ) { $line = $_ if $_; } print $line,"\n"; # alternatively print +(split /\n/, $string)[-1],"\n";
Re: Finding the last statement in string of atmost similar pattern
by Rahul6990 (Beadle) on May 06, 2013 at 07:33 UTC
    Use the below step:
    1. Define your pattern, do not for get to put '()' around the pattern.
    2. Open the file.
    3. Apply pattern on each line.
    4. After reading the entire file heck '$1' variable, the last matched pattern will present in it.


    For help use this link.
Re: Finding the last statement in string of atmost similar pattern
by hdb (Monsignor) on May 06, 2013 at 08:17 UTC

    If there are more lines to follow in your file and you want to automatically detect the end of the pattern without specifying the pattern explicitly, you can use the module String::Similarity, for example:

    use strict; use warnings; use String::Similarity; my $string; $string .= "abc($_) = xyz(".($_-1).");\n" for 1..10; $string .= "not similar\n"; my @lines = split /\n/, $string; for (1..$#lines) { print "\"".$lines[$_-1]."\" == \"".$lines[$_]."\" ? "; print similarity $lines[$_-1], $lines[$_]; print "\n"; }

    gives the following output

    "abc(1) = xyz(0);" == "abc(2) = xyz(1);" ? 0.875 "abc(2) = xyz(1);" == "abc(3) = xyz(2);" ? 0.875 "abc(3) = xyz(2);" == "abc(4) = xyz(3);" ? 0.875 "abc(4) = xyz(3);" == "abc(5) = xyz(4);" ? 0.875 "abc(5) = xyz(4);" == "abc(6) = xyz(5);" ? 0.875 "abc(6) = xyz(5);" == "abc(7) = xyz(6);" ? 0.875 "abc(7) = xyz(6);" == "abc(8) = xyz(7);" ? 0.875 "abc(8) = xyz(7);" == "abc(9) = xyz(8);" ? 0.875 "abc(9) = xyz(8);" == "abc(10) = xyz(9);" ? 0.848484848484849 "abc(10) = xyz(9);" == "not similar" ? 0.0714285714285714
Re: Finding the last statement in string of atmost similar pattern
by Anonymous Monk on May 06, 2013 at 07:10 UTC
    and then what happened?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2024-04-19 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found