#!/usr/bin/perl # http://perlmonks.org/?node_id=1202179 use strict; use warnings; use Data::Dumper; chomp( my @words = ); # find the "banks" in the word list my %banks = map { $_, [ ] } grep !/(.).*\1/, @words; print Data::Dumper->Dumpxs( [ \ @words, \ %banks ], [ qw{ *words *banks } ] ); # 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 #### @words = ( 'ab', 'aabb', 'aaabbb', 'aaabbab', 'xxyy', 'xxxyyy' ); %banks = ( 'ab' => [] ); ab -> ab aabb -> ab aaabbb -> ab aaabbab -> abab xxyy -> xy xxxyyy -> xy ab aabb aaabbb