Think this might be a start ? .. only tested on your data: ;))
#!/usr/bin/perl -w
use strict;
sub FindRoot;
my %Word=map {chomp;split /,/} <>;
for (keys %Word)
{
print "Word: $_, etc: $Word{$_}\n";
};
my $Root=FindRoot("Longest",keys %Word);
my @Words=map {split} (values %Word);
my $Word=FindRoot("Shortest",@Words);
print "\n---\n$Root:$Word\n";
for (keys %Word)
{
print "-",substr($_,length$Root),":";
print "",substr($Word{$_},0,rindex($Word{$_},$Word)),"\n";
};
sub FindRoot
{
my $Order=shift;
my %Container;
my $Min="xxxxxxxxxxxxxxx";
print "Finding $Order\n";
if ($Order eq "Shortest")
{
$Min="";
$Min=((length $_)>(length $Min))?$_:$Min for (@_);
}
else { $Min=((length $_)<(length $Min))?$_:$Min for (@_); };
for my $Word (@_)
{
$Container{substr lc $Word,0,$_}++ for(2..length $Min);
};
my $Root="";
$Min=0;
my @List=sort keys %Container;
@List=reverse@List if ($Order eq "Longest");
print "List: @List\n";
for (@List)
{
if ($Container{$_}>$Min)
{
$Root=$_;
$Min=$Container{$_};
};
};
return $Root;
};
It`s probably not the cleanest code, but it was fun to do, so
I thought I`d post it :)))
I figure you can use this as a start to really solve your
problem, it shouldn`t be too hard to once you got the Root,
take the substring out of the hash, and start looking for the next
one (using that FindRoot sub) which would give you `lar` ,
making the next root `BasLar` ...
Hmmmmm... actually I think I got that to work here...
Looking forward to more test-data :))
GreetZ!,
print "profeth still\n" if /bird|devil/;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.