in reply to Find the common beginning substring in an array of strings

Updated: To correct odd number problem identified my mbethke and AnomalousMonk below.

@array_of_test_names = ( ... );; $mask = '';; $mask ^= $_ for @array_of_test_names;; $mask ^= $array_of_test_names[0] if @array_to_test_names &1' $mask =~ m[^(\0+)] and $len = length( $1 );; print substr $array_of_test_names[ 0 ], 0, $len;; History - Family History - Suite with Patient - Clinic -

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

  • Comment on Re: Find the common beginning substring in an array of strings (Updated)
  • Download Code

Replies are listed 'Best First'.
Re^2: Find the common beginning substring in an array of strings
by mbethke (Hermit) on Jun 06, 2012 at 18:16 UTC

    Pretty smart and super fast, only works with an even number of strings though. Off the top of my head I'd say for an uneven number just duplicate one of the strings.

    Gotta go and pick up my son from kindergarten, otherwise there'd be code to support my claim :)

      I've updated my node with a correction. Thanks.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

Re^2: Find the common beginning substring in an array of strings
by AnomalousMonk (Archbishop) on Jun 06, 2012 at 18:27 UTC

    But that doesn't work for arrays with odd numbers of elements:

    >perl -wMstrict -MData::Dump -le "my @array_of_test_names = qw(ABCxyz ABCDEfoo ABCDbar); ;; my $mask = '';; $mask ^= $_ for @array_of_test_names;; dd $mask; ;; my $len; $mask =~ m[^(\0+)] and $len = length( $1 );; my $min = substr $array_of_test_names[ 0 ], 0, $len;; print qq{'$min'}; " "ABCx^}\35o" Use of uninitialized value $len in substr at -e line 1. ''

    Update: I didn't see mbethke's post until after I posted, but I agree that simple fix will solve the odd-element problem.

Re^2: Find the common beginning substring in an array of strings (Updated)
by brx (Pilgrim) on Jun 07, 2012 at 11:50 UTC

    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";