Perhaps I'm misunderstanding something but it seems to me that my $banksto = tr///csr; is only removing adjacent duplicate letters. Adapting your code:-
#!/usr/bin/perl
# http://perlmonks.org/?node_id=1202179
use strict;
use warnings;
use Data::Dumper;
chomp( my @words = <DATA> );
# find the "banks" in the word list
my %banks = map { $_, [ ] } grep !/(.).*\1/, @words;
print Data::Dumper->Dumpxs( [ \ @words, \ %banks ], [ qw{ *words *bank
+s } ] );
# find what a word "banks" to and save if "bank" exists
for ( @words )
{
my $banksto = tr///csr;
print qq{$_ -> $banksto\n};
exists $banks{$banksto} and push @{ $banks{$banksto} }, $_;
}
print "@$_\n" for values %banks;
__DATA__
ab
aabb
aaabbb
aaabbab
xxyy
xxxyyy
produces the following:-
@words = (
'ab',
'aabb',
'aaabbb',
'aaabbab',
'xxyy',
'xxxyyy'
);
%banks = (
'ab' => []
);
ab -> ab
aabb -> ab
aaabbb -> ab
aaabbab -> abab
xxyy -> xy
xxxyyy -> xy
ab aabb aaabbb
My reading of the OP's requirement would imply that the added 'aaabbab' should also bank to 'ab' and e.g., 'cabbage' banks to 'cabge' not 'cabage' which is what comes out of tr//csr. What am I misunderstanding?
Update: Thinking about it further, I realised I was fixating on the tr//csr in tybalt89's post (which usage I had never encountered before) and ignoring the join "", sort split //, $key used by the OP. That is what I was misunderstanding :-/
|