我将您的问题理解为
给定一个单词列表,我们想要删除所有作为其他单词子串的单词。
这是一个通用的 Perl 解决方案:
sub weed_out {
my @out;
WORD:
while (my $current = shift) {
for (@_) {
# skip $current word if it's a substring of any other word
next WORD if -1 != index $_, $current;
}
push @out, $current;
}
return @out;
}
请注意,我们shift
来自@_
参数数组,因此内部循环每次都会变短。
如果我们在执行内部循环时遇到一个单词的子字符串$current
,我们实际上可以通过以下方式删除它splice
:
WORD:
while (my $current = shift) {
for (my $i = 0; ; $i++) {
last unless $i <= $#_; # loop condition must be here
# remove the other word if it's a substring of $current
splice(@_, $i, 1), redo if -1 != index $current, $_[$i];
# skip $current word if it's a substring of any other word
next WORD if -1 != index $_[$i], $current;
}
push @out, $current;
}
但我宁愿以“优化”为基准。
如果需要,这可以很容易地嵌入到 shell 脚本中:
$ perl - <<'END' FILE
my @words = <>;
chomp(@words);
WORD: while (my $current = shift @words) {
for (@words) {
# skip $current word if it's a substring of any other word
next WORD if -1 != index $_, $current;
}
print "$current\n";
}
END