Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Finding the last statement in string of atmost similar pattern

by amma (Novice)
on May 06, 2013 at 07:05 UTC ( #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);

Comment on Finding the last statement in string of atmost similar pattern
Download Code
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?
Re: Finding the last statement in string of atmost similar pattern
by hdb (Prior) 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 (Prior) 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

Log In?
Username:
Password:

What's my password?
Create A New User
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? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2014-10-20 05:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (72 votes), past polls