我仍然试图在排列匹配上工作,我不知道是否有人有更好的方法来做到这一点。我想以任何顺序匹配数组中的所有模式,即匹配数组中的项(字符串或其他对象)的排列。例如,如果数组是(1,2,3),那么如果一个字符串以任意顺序包含1和2以及3,则为真;即如果一个字符串包含(1,2,3)的排列,则为真。perl6排列匹配
我现在是这样的:
my @x = < one eins uno yi two zwei dos er one one one two two two >;
my @z = < one eins uno yi two zwei dos er one one one two two two foo >;
my $y = "xxx one eins uno yi two zwei dos er xxx";
sub matchAllWords($aString, @anArray) {
my $arraySize = @anArray.elems;
if $arraySize == 0 { False; }
elsif $arraySize == 1 {
($aString.match(/:i "@anArray[0]" /)).Bool;
} else {
my $firstCheck = ($aString.match(/:i "@anArray[0]"/)).Bool;
if $firstCheck {
$firstCheck
and
(matchAllWords($aString, @anArray[1..*]));
} else {
return False;
}
}
}
say matchAllWords($y, @x);
# result is True, but it should NOT be True because $y should not
# match permutations of @x which contains multiple identical elements
# of "one" and "two"
say matchAllWords($y, @z); # False as expected;
的问题是,我的所有功能独特的单词相匹配的阵中,但无法区分的重复的单词排列。我可以添加越来越多的代码来判断一个单词是否已经匹配,但是更多的代码来完成一个简单的想法,即“排列匹配”,是不可逾越的。有什么建议么?由于
我不清楚你想达到什么目的。你是否想要检查Array'n'次中包含的每个元素是否也出现在字符串中,正好是'n'次?或者是其他东西? – smls
你看了,'袋'和设置操作员?感觉就像你想以某种方式比较两个包(身份/子集)。那是你要的吗? –
难道你只是排序两个列表? –