Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Is there any API available in perl to find longest common substring from two strings

by rad_144 (Initiate)
on Dec 31, 2012 at 08:28 UTC ( #1011019=perlquestion: print w/ replies, xml ) Need Help??
rad_144 has asked for the wisdom of the Perl Monks concerning the following question:

I have a requirement of matching the files installed in different environments and do some operation based on that. For eg: Lets say I have a file called  domains/abcd/xyz/1.txt . In two different installed environments, this file is available at
env 1 :
 /home/instance/domains/abcd/xyz/1.txt
env 2 :
 /a1234/domains/abcd/xyz/1.txt
paths. Now I have the env 3 in which I have this file at  /a1234/topfolder/instance/domains/abcd/xyz/1.txt
If observed, the three environments have the identical file installed in different parent folders. So my question is how can I determine that  /a1234/topfolder/instance/domains/abcd/xyz/1.txt is identical to  /home/instance/domains/abcd/xyz/1.txt
and  /a1234/domains/abcd/xyz/1.txt . My idea is that if I can find the longest common substring of above three filenames, I can have the solution to my requirement. Is there any API available in perl to find longest common substring of two strings? or is there any other solution ?

Comment on Is there any API available in perl to find longest common substring from two strings
Select or Download Code
Re: Is there any API available in perl to find longest common substring from two strings
by Athanasius (Monsignor) on Dec 31, 2012 at 08:38 UTC
      Thanks for the code snippets. It really helped.
Re: Is there any API available in perl to find longest common substring from two strings
by RichardK (Priest) on Dec 31, 2012 at 11:34 UTC

    You could use the File::Spec functions: spitpath & splitdir, to simplify your matching.

    ($volume,$directories,$file) = File::Spec->splitpath( $path ); @dirs = File::Spec->splitdir( $directories );

    then it's simple to compare the arrays to see how much they have in common.

Re: Is there any API available in perl to find longest common substring from two strings
by LanX (Canon) on Jan 03, 2013 at 15:51 UTC
    I don't think you need the longest common substring, but the longest common tail of dirs.

    here some code, it basically starts from the end and stops as soon as not all dirs are identical.

    (can be done far more elegantly but lost interest ;-)

    HTH, TIMTOWTDI!

    use strict; use warnings; use Data::Dump qw(pp); my @paths = map { [ split '/' ] } qw[ /home/instance/domains/abcd/xyz/1.txt /a1234/domains/abcd/xyz/1.txt /a1234/topfolder/instance/domains/abcd/xyz/1.txt ]; #pp @paths; my $a_norm = shift @paths; my @result; dir_loop: for my $idx ( 1 .. @$a_norm ) { for my $a_path (@paths){ last dir_loop if $a_norm->[-$idx] ne $a_path->[-$idx]; } unshift @result,$a_norm->[-$idx] } pp @result;

    output:

    ("domains", "abcd", "xyz", "1.txt")

    Cheers Rolf

      Could you please elaborate how this work?
        Could you please elaborate which part is unclear ?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (20)
As of 2014-10-20 18:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (88 votes), past polls