Re: Finding the common textual denominator
On Thu, Mar 10, 2005 at 04:10:33PM -0300, Maximiliano Combina wrote:
>
> On Sun, 2005-03-06 at 02:16 +0100, Olle Eriksson wrote:
> > Can anyone help me with how to find the common textual denominator of an
> > array of strings. I have been searching the web and the man pages of
> > grep, awk etc to no avail.
> >
> > Given the following list of directory names I want to have a script return
> > "Eric_Clapton".
> >
> > Eric_Clapton-Big_Boss_Man-2CD-Retail-2002-DGN/
> > Eric_Clapton-Higher_Ground-(CDS)-2003-RNS/
> > Eric_Clapton_-_Me_and_Mr_Johnson-(PROPER)-CD-2004-TN/
> > Eric_Clapton-One_More_Car_One_More_Rider-2CD-2002-RNS/
> > Eric_Clapton - Pilgrim/
> >
> > Regards
> > Olle
Here's a bit o' perl to do this, however crudely, by creating
a regular expression pattern from the :
#!/usr/bin/perl -w
use strict;
my $ltd = <DATA>; # read the first entry
while ( <DATA> ) {
$ltd =~ s/(.)/$1?/g; # add "?" after each character
($ltd) = m/^($ltd)/x; # matches entry against m/^(E?r?i?c?...)/
# print "so far: $ltd\n";
}
print qq(the leading common textual denominator is "$ltd"\n);
__END__
Eric_Clapton-Big_Boss_Man-2CD-Retail-2002-DGN/
Eric_Clapton-Higher_Ground-(CDS)-2003-RNS/
Eric_Clapton_-_Me_and_Mr_Johnson-(PROPER)-CD-2004-TN/
Eric_Clapton-One_More_Car_One_More_Rider-2CD-2002-RNS/
Eric_Clapton - Pilgrim/
>
> i hve read all the thread.
> the common textual denominator you are looking for... is always at the
> beginning?
> a more interesting problem (i dont konw if useful) is to search the
> denominator _anywhere_ in the strings.
> for example:
> aa XXXgg
> bb fgXXXbb
> ffgj XXX hhss
>
> should return XXX
It seems like the code above (without the ^ anchor) ought to work
for this, too, but I must be missing something...
Ken
--
Ken Irving, fnkci@uaf.edu
Reply to: