Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Find common prefix from a list of strings

by Jenda (Abbot)
on Jul 14, 2003 at 20:20 UTC ( #274146=note: print w/replies, xml ) Need Help??


in reply to Find common prefix from a list of strings

@files = qw(model4run1 model2run1 model4run2 model1run1); my $first = shift(@files); my $combined = $first; foreach (@files) { $combined &= $_; } $combined ^= $first; $combined =~ s/[^\x00].*//; my $prefix = substr($first, 0, length($combined)); print qq{Prefix is "$prefix"\n};
First I AND together all the elements of the array (therefore only bits that are the same for all items are set), then I XOR the result with the first element to get zeroes for all characters that are the same, then strip everything from the first nonzero character and last get as many characters from the first element as you have zeroes.

Update: You can of course replace the my $first = shift(@files); by my $first = $files[0];, the result will be the same and @files will be preserved.

Update: The code is wrong. It returns an incorrect result for

@files = qw(model4run1 model5run1);
See a fixed version here. It makes my solution a little slower, but it's still the fastest.

Jenda
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
   -- Rick Osborne

Edit by castaway: Closed small tag in signature

Replies are listed 'Best First'.
Re: Re: Find common prefix from a list of strings
by fletcher_the_dog (Friar) on Jul 14, 2003 at 21:57 UTC
    Here is a sort version of Jenda's solution
    my @files = qw(model4run1 model2run1 model4run2 model1run1); my $same = $files[0]; $same &=$_ for @files; ($same) = split 0x00, $same,2; print "Prefix is \"$same\"\n";

      Uh oh ... I wanted to point out that your solution is wrong, found a conter example and ... it broke my code as well :-(

      Here is a fixed solution:

      @files = qw(model4run1 model5run1); #@files = qw(model4run1 model2run1 model4run2 model1run1); my $first = shift(@files); my $and = $first; my $or = $first; foreach (@files) { $and &= $_; $or |= $_; } my $combined = $and ^ $or; $combined =~ s/[^\x00].*//; my $prefix = substr($first, 0, length($combined)); print qq{Prefix is "$prefix"};

      Jenda
      Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
         -- Rick Osborne

      Edit by castaway: Closed small tag in signature

        There's another case this will fail for:

        @files = qw(model4run1 model2run1 model4run2 abbot); ... print Jenda(@files),$/; __DATA__ model4

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1

        Hmm, well I guess that is what I get for only doing testing on the example array. I guess I misunderstood how the & worked on strings, I thought it did it on a byte/character basis and only on bit basis for numbers. Thanks for finding this counter example.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2020-01-18 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?