#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; # Filter out all words that can't be candidates one way or the other my @candidates=grep { m{[RS]} } ; chomp(@candidates); # Sort them in order of length my @orderedCandidates=sort{ length $a <=> length $b } @candidates; # Push a guard on the end push @orderedCandidates,""; warn Data::Dumper->Dump([\@orderedCandidates],[qw(*orderedCandidates)]),' '; my %hash; my $length=length($orderedCandidates[0]); for my $word (@orderedCandidates) { if (length($word) == $length) { # Same length ... add it to the hash $hash{$word}=undef; } else { # Hash is complete for my $Rword (grep { m{R} } keys %hash) { # Word has a R my $pos=0; while (($pos=index($Rword,'R',$pos)) >= 0) { # Found an R # Make its S equivalent my $Sword=$Rword; substr($Sword,$pos,1)='S'; print "$Rword - $Sword\n" if (exists $hash{$Sword}); $pos++ } } # Done with this hash --- so start over %hash=(); $hash{$word}=undef; $length=length($word); }; } __DATA__ ONE THREE FOUR FOUS RRRRRRRR RSRRRRRS RRSRRRRR