in reply to Re: Print left-anchored similarities between two strings
in thread Print left-anchored similarities between two strings

You can save a couple of steps:

my @strings = qw( /home/usernames/doejohnwilson x/home/usernames/doejanemary ); my $common_length = ( $strings[0] ^ $strings[1] ) =~ /^\0*/ && $+[0]; print substr $strings[0], 0, $common_length;

Replies are listed 'Best First'.
Re^3: Print left-anchored similarities between two strings
by miketosh (Acolyte) on Nov 11, 2009 at 20:27 UTC
    Awesome!

    I think this will work for me (since I can have an unspecified number of elements in an array...)

    # our $input is user inputted string # and I only want directory/ names our @matches = map { -d $_? $_ .="/" : "" } <$input*>; expand_match || print "\a"; sub expand_match { return $input = $matches[0] if(@matches == 1); # Get the longest matching string from all elements my $match = $matches[0]; for (@matches) { $match = substr $_, 0, ( $match ^ $_ ) =~ /^\0*/ && $+[0]; } return $input = $match if length($match) > length($input); return 0; }