嘿,我想知道如何使这段代码起作用。基本上,我希望保留$filename行,只要它们在路径中包含$user:

        open STDERR, ">/dev/null";
        $filename=`find -H /home | grep $file`;
        @filenames = split(/\n/, $filename);
        for $i (@filenames) {
            if ($i =~ m/$user/) {
                #keep results
            } else {
                delete  $i; # does not work.
            }
        }
        $filename = join ("\n", @filenames);
        close STDERR;

我知道您可以像delete $array[index]一样删除,但是我没有这种循环的索引。

最佳答案

您可以将循环替换为:

@filenames = grep /$user/, @filenames;

09-29 21:39