Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Find common prefix from a list of strings (tye)

by tye (Sage)
on Jul 14, 2003 at 19:55 UTC ( #274133=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

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

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