我仍然試圖在排列匹配上工作,我不知道是否有人有更好的方法來做到這一點。我想以任何順序匹配數組中的所有模式,即匹配數組中的項(字符串或其他對象)的排列。例如,如果數組是(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
你看了,'袋'和設置操作員?感覺就像你想以某種方式比較兩個包(身份/子集)。那是你要的嗎? –
難道你只是排序兩個列表? –