Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Find the common beginning substring in an array of strings (Updated)

by brx (Pilgrim)
on Jun 07, 2012 at 11:50 UTC ( [id://974922]=note: print w/replies, xml ) Need Help??


in reply to Re: Find the common beginning substring in an array of strings (Updated)
in thread Find the common beginning substring in an array of strings

Not sure if I miss something, but it seems there is a problem.

With this array for example:

my @array_of_test_names = ( "History--abcdef", "History--aaa", "History--123", "History--1X", );
output: History--a

updated: bad try

A try:

my $mask = ''; $mask ^= $_ ^ $array_of_test_names[0] for @array_of_test_names; $mask =~ m[^(\0+)] and $len = length( $1 ); print substr $array_of_test_names[ 0 ], 0, $len; print "\n";

update2: btw, a not pretty solution

my $res = ''; for my $i ( 0 .. (length($array_of_test_names[0]) -1)) { my $letter = substr ($array_of_test_names[0],$i,1); last if ( scalar grep { $letter ne substr($array_of_test_names[$_] +,$i,1)} (0 .. $#array_of_test_names)); $res .= $letter; }; print $res,"\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-26 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found