對於文件篩選器,我想使用一個單詞數組,其中的行將被檢查,如果它們匹配任何單詞。Perl,搜索字符串,用於發生數組的項目
我已經有這個一個相當簡單的方法(僅基本匹配的部分):
# check if any of the @words is found in $term
@words= qw/one
two
three/;
$term= "too for the show";
# the following looks very C like
$size= @words;
$found= 0;
for ($i= 0; $i<$size && !$found; $i++) {
$found|= $term=~ /$words[$i]/;
}
printf "found= %d\n", $found;
我們已經看到了很多在Perl神祕的語法和解決方案,我不知道是否(或相當的)是更緊湊的寫作方式。
['從數據list2re' :: Munge時間(https://metacpan.org/pod/Data::Munge#list2re-LIST)不會的東西非常相似,但也可以處理一些邊緣情況。來自OP的 – melpomene
+1。我喜歡這個(這種類型顯示了我期待的Perl的aracane方式)。但是裝配的那個更適合我的需求。謝謝回答。 – Terminality