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

Re: Find common prefix from a list of strings

by Zaxo (Archbishop)
on Jul 15, 2003 at 01:12 UTC ( #274246=note: print w/replies, xml ) Need Help??


in reply to Find common prefix from a list of strings

Wanting the common prefix makes for a nifty shortcut. Sort the strings alphabetically and you only need to compare the first and last of them,

sub common_prefix { my ($first, $last) = (sort @_)[0,-1]; my $i = 0; while (substr($first, $i, 1) eq substr($last, $i, 1)) { $i++} substr $first, 0, $i; }

After Compline,
Zaxo

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2020-10-29 11:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (270 votes). Check out past polls.

    Notices?