2015-06-17 123 views
0

我想要清除所有元素,将它们存储在数组中,然后从该数组中删除符号链接。问题是我不知道如何删除一个数组中包含在另一个数组中的所有元素,因为我是perl的新手。从perl中删除另一个数组中的一个数组中的元素

贝娄是我的代码到目前为止。

foreach ${dir} (@{code_vob_list}) 
{ 
    ${dir} =~ s/\n//; 
    open(FIND_FILES, "$cleartool find ${dir} -type f -exec 'echo \$CLEARCASE_PN' |") or die "Can't stat cleartool or execute : $!\n"; #This command gets all files 
    @{files_found} = <FIND_FILES>; 

    open(SYMBOLIC_FIND_FILES, "$cleartool find ${dir} -type l -exec 'echo \$CLEARCASE_PN' |") or die "Can't stat cleartool or execute : $!\n"; #This command get all symbolic links 
    @{symbolic_files_found} = <SYMBOLIC_FIND_FILES>; 
    #Filter away all strings contained in @{symbolic_files_found} from @{files_found} 
    foreach my ${file} (@{files_found}) 
    { 
     #Here I will perform my actions on @{files_found} that not contains any symbolic link paths from @{symbolic_files_found} 
    } 
} 

在此先感谢

+1

你在哪里学会把所有的标识符放在花括号里面?我希望你意识到这是不必要的,我相信它会使代码更不易读 – Borodin

+0

尝试查看File :: Find查找这样的任务。 –

回答

3

要过滤的数组,你可以使用grep

my @nonlinks = grep { my $f = $_; 
         ! grep $_ eq $f, @symbolic_files_found } 
       @files_found; 

但它通常是清洁剂使用哈希。

my %files; 
@files{ @files_found } =();   # All files are the keys. 
delete @files{ @symbolic_files_found }; # Remove the links. 
my @nonlinks = keys %files; 
1

我建议您安装并使用List::Compare。该代码是这样的

正如我在评论中写道,我不知道如果你喜欢写你的标识符这样的,我也不清楚,如果你避免反引号`...`(同qx{...})有利于管道的开放是有原因的,但是这是更接近如果你喜欢我如何编写代码

get_unique有一个同义词get_Lonly,你可能会发现更多的表现

use List::Compare; 

for my $dir (@code_vob_list) { 

    chomp $dir; 

    my @files_found = qx{$cleartool find $dir -type f -exec 'echo \$CLEARCASE_PN'}; 
    chomp @files_found; 

    my @symbolic_files_found = qx{$cleartool find $dir -type l -exec 'echo \$CLEARCASE_PN'}; 
    chomp @symbolic_files_found; 

    my $lc = List::Compare->new('--unsorted', \@files_found, \@symbolic_files_found); 
    my @unique = $lc->get_unique; 
} 
相关问题