in reply to An efficient way to gather a common portion of several strings' beginnings
Assuming none of your strings contain nulls:
use strict; use warnings; my @strings = ( 'string that I need to gather the common base from: number 1 and s +ome other junk in it', 'string that I need to gather the common base from: number 2 and s +ome other junk in it', 'string that I need to gather the common base number 4 and some ot +her junk in it', 'string that I need to gather the common base from: number 3 and s +ome other junk in it', ); my $common = $strings[0]; for my $str (@strings[1 .. $#strings]) { ($common ^ $str) =~ m/^\0*/; $common = substr $str, 0, $+[0] if $+[0] < length $common; } print "'$common'";
Prints:
'string that I need to gather the common base '
The xor operator ('^') combines the strings byte by byte and generates a null for each identical byte pair. @+ contains the offsets of the ends of matches. In this case the entire match is just the ticket so we use the first entry, 0, which is effectively the length of the common base.
<Update: Fixed match issue pointed out by AnomalousMonk.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: An efficient way to gather a common portion of several strings' beginnings
by AnomalousMonk (Archbishop) on Nov 15, 2015 at 18:26 UTC |