我似乎无法从“文件夹”数组中删除“ExcludedFolders”值,这里是我所拥有的,任何帮助,谢谢。
CODE:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
my @ExcludedFolders = qw(
/this/is/folder/path/one/exclude
/this/is/folder/path/three/exclude
/this/is/folder/path/five/exclude
);
my @Folders = qw(
/this/is/folder/path/one/exclude/some/file.dat
/this/is/folder/path/two/exclude/some/file.dat
/this/is/folder/path/three/exclud/some/file.dat
/this/is/folder/path/four/excludee/some/file.dat
/this/is/folder/path/five/exclude/some/file.dat
/this/is/folder/path/six/exclude/some/file.dat
);
my %remove;
@remove{@ExcludedFolders}=();
@Folders=grep{!exists$remove{$_}}@Folders;
foreach my $Path (@Folders) {
print "$Path \n";
}
由于
答案 0 :(得分:3)
它不起作用,因为'/this/is/folder/path/one/exclude/some/file.dat'
哈希中不存在%remove
; '/this/is/folder/path/one/exclude/'
确实如此。
我建议在这里使用Tie::RegexpHash
:
use strict;
use warnings;
use Tie::RegexpHash;
my $remove = Tie::RegexpHash->new;
my @ExcludedFolders = qw(
/this/is/folder/path/one/exclude
/this/is/folder/path/three/exclude
/this/is/folder/path/five/exclude
);
$remove->add( qr/^$_/ , 1 ) foreach @ExcludedFolders;
my @Folders = qw(
/this/is/folder/path/one/exclude/some/file.dat
/this/is/folder/path/two/exclude/some/file.dat
/this/is/folder/path/three/exclud/some/file.dat
/this/is/folder/path/four/excludee/some/file.dat
/this/is/folder/path/five/exclude/some/file.dat
/this/is/folder/path/six/exclude/some/file.dat
);
@Folders = grep { ! $remove->match_exists( $_ ) } @Folders;
答案 1 :(得分:1)
两个阵列中没有重叠。 @Folders
被错误命名,因为它确实是文件。我确信有更好的方法,但我很忙:
## build a big regex to match exclude directories against
my $exclude_re = join '|', map { "\A\Q$_\E" } @ExcludedFolders;
my @filtered_files = grep { $_ !~ m{$exclude_re}i } @Folders;
答案 2 :(得分:1)
对于@Folders
中的每个路径,您需要检查是否有任何排除的路径与初始段匹配。 List::MoreUtils::none在这里很方便,但很容易被其他grep
替换:
#!/usr/bin/env perl
use strict; use warnings;
use List::MoreUtils qw( none );
my @ExcludedFolders = map qr{^$_}, qw'
/this/is/folder/path/one/exclude
/this/is/folder/path/three/exclude
/this/is/folder/path/five/exclude
';
my @Folders = qw'
/this/is/folder/path/one/exclude/some/file.dat
/this/is/folder/path/two/exclude/some/file.dat
/this/is/folder/path/three/exclud/some/file.dat
/this/is/folder/path/four/excludee/some/file.dat
/this/is/folder/path/five/exclude/some/file.dat
/this/is/folder/path/six/exclude/some/file.dat
';
@Folders = grep {
my $folder = $_;
none {
$folder =~ $_
} @ExcludedFolders
} @Folders;
use Data::Dumper;
print Dumper \@Folders;
输出:
$VAR1 = [ '/this/is/folder/path/two/exclude/some/file.dat', '/this/is/folder/path/three/exclud/some/file.dat', '/this/is/folder/path/four/excludee/some/file.dat', '/this/is/folder/path/six/exclude/some/file.dat' ];