Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Find common prefix from a list of strings

by Anonymous Monk
on Jul 14, 2003 at 20:49 UTC ( #274162=note: print w/replies, xml ) Need Help??


in reply to Find common prefix from a list of strings

There's a functional module somewhere with reduce in it, I think.

Anyway, you said map, here's my solution...

my @ans = grep {$_} map { my $func = $_; reduce( sub{ my($first, $second) = @_; return ($first eq $second) && $first; } , map(&$func, @data)); } map { my $ctr = $_; sub { my $str = $_; substr($str, 0, $ctr); } } (1..length($data[0])); print pop(@ans), "\n";

I faked a reduce to test it, it works on this dataset.

Replies are listed 'Best First'.
Re: Re: Find common prefix from a list of strings
by tilly (Archbishop) on Jul 14, 2003 at 22:16 UTC
    You're right that there is a functional module with reduce in it.

    It is called List::Util.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2020-01-25 05:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?