http://www.perlmonks.org?node_id=274133


in reply to Find common prefix from a list of strings

sub FindCommonPrefix { my $model= pop @_; my $len= length($model); for my $item ( @_ ) { my $dif= $model ^ substr($item,0,$len); $len= length( ( $dif =~ /^(\0+)/ )[0] ); substr( $model, $len )= ""; } return $model; }
                - tye

Replies are listed 'Best First'.
Re: Re: Find common prefix from a list of strings (tye)
by sauoq (Abbot) on Jul 14, 2003 at 20:41 UTC

    Nice solution; ++tye.

    Changing

    $len= length( ( $dif =~ /^(\0+)/ )[0] );
    to
    $len= length( ( $dif =~ /^(\0+)/ )[0] || '' );
    will make it run quiet under warnings when the common prefix is the empty string.

    -sauoq
    "My two cents aren't worth a dime.";
    

      Oops. That + should have been a *. Thanks for the hint. (:

                      - tye