2015-06-17 185 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; 
} 
相關問題