我是perl的新手。任何人都可以举出一个perl代码示例,以使用哈希值检测给定字符串之间的字谜。两个字符串-游泳池和马球。
答案 0 :(得分:1)
sub key(_) { join "", sort split //, $_[0] }
if (key("pool") eq key("polo") {
say "Pool and polo are anagrams of each other.";
} else {
say "Pool and polo aren't anagrams of each other.";
}
如果您有字典,
sub key(_) { join "", sort split //, $_[0] }
my $dict_qfn = "...";
my $search = "pool";
my %anagrams;
{
open(my $fh, '<', $dict_qfn)
or die("Can't open \"$dict_qfn\": $!\n");
while (<$fh>) {
chomp;
push @{ $anagrams{ key($_) } }, $_;
}
}
my @results = grep { $_ ne $search } @{ $anagrams{$search} // [] };
say "Anagrams of $search: ".( @results ? "@results" : "[none]" );