2011-04-15 64 views
-1

好吧,所以我想採取一個散列,如果在數組中的任何字符串包含密鑰(不值實際的密鑰名稱)在哈希丟棄它。否則打印出字符串。這個問題與findHidden子例程的一部分有關。我嘗試了很多不同的東西,我會在下面評論我遇到的問題。我敢肯定有人有一個答案,總是拿一個在堆棧溢出:)需要幫助的Perl程序

#!/usr/bin/perl 
# Configure 
use strict; 
use warnings; 
use Data::Dumper; 
# 
sub findHidden; 
sub GetInfo; 
sub defineHash; 
############## 

$passwd = '/etc/passwd'; 
%info =(); 

sub GetInfo { 
     die "Cannot open: $passwd" 
     unless (open(PW,$passwd)); 
     while(<PW>) { 
       chomp; 
       my ($uname,$junk1,$junk2,$junk3,$domain,$home) = split(':', $_); 
       next unless ($home =~ /vs/); 
       %info = (
         domain => $domain, 
         home => "$home/", 
         tmp  => "$home/tmp", 
         htdocs => "$home/www/htdocs", 
         cgibin => "$home/www/cgi\-bin", 
       ); 
       print "\n" . $info{domain} . "\n"; 
       print "+"x40,"\n\n"; 
       findHidden($info{tmp}); 
     } 
} 
sub findHidden { 
     defineHash; 
     print "Searching " . $_[0] . "\n"; 
     print "-"x30,"\n\n"; 
     @hidden = `find $_[0] -iname ".*"`; 
     for(@hidden) { 
       foreach $key (keys % hExcludes) { 
         if ($_ =~ /$key/){ # 
           last;   # This portion is 
         }else{    # Only an issue when using more 
           print "$_"; # than 2 keys in my hash. 
           last; 
         } 
       } 
     } 
} 
sub defineHash { 
     %hExcludes =(); 
     %hExcludes = map { $_, 1 } (
       'spamd','.nfs'  # If I add another key here, it breaks. 
     ); 

     %knownExploits = 
       (); 
     print Dumper \%hExcludes; 
} 
GetInfo; 

這工作,並打印出這樣的事:

/somedir/tmp/.testthis
/somedir/tmp/.sdkfbsdif
/somedir/tmp/.asdasdasd

我明白爲什麼它不工作,因爲它循環的鑰匙有些是錯誤的,有些是積極的,我只是想不出如何讓它做我想做的事情,請假設我可能想要你10把鑰匙。我知道有一些方法可以在我的排除項中不使用散列鍵值的情況下實現,但這是我想要完成的。

我也試過shift @hidden如下無濟於事。

   foreach $key (keys % hExcludes) { 
         if ($_ =~ /$key/){ # 
           last;   # 
           shift @hidden;# This portion is 
         }else{    # Only an issue when using more 
           print "$_"; # than 2 keys in my hash. 
           last; 
         } 

另外,請記住,只有事情停止工作,當我添加第三個......或多個鍵。

 %hExcludes = map { $_, 1 } (
       'spamd','.nfs','key3'  # If I add another key here, it breaks 
     ); 
+1

你只需要說出你的意思是什麼「它打破」?一個提示:你正在替代其值在正則表達式中包含元字符的變量,即「.nfs」。您可能需要編寫/ \ Q $ key \ E /以提供報價。 – Ingo 2011-04-15 23:32:06

+3

-1你發佈的代碼不可能「工作」,因爲它甚至沒有編譯。您應該確保發佈與您運行的代碼完全相同的代碼! – tadmc 2011-04-16 00:06:37

回答

3

你需要的是這樣的:

@hidden = `find $_[0] -iname ".*"`; 
    for(@hidden) { 
     undef $isExcluded; 
     foreach $key (keys % hExcludes) { 
      if ($_ =~ /$key/){ 
       $isExcluded=1; 
       last; 
      } 
     } 
     if(! $isExcluded) { 
      print "$_"; 
     } 
    } 

無論通過hExcludes的按鍵您的掃描發生,代碼的第一個關鍵遇到last,並沒有處理任何更多。你需要設置一個標誌並繼續迭代,直到沒有更多的鍵被設置,或者找到匹配。然後你可以打印出不匹配的值。

+0

你真棒,像魅力一樣工作 – noledgeispower 2011-04-16 00:07:10

+0

問題,你認爲這種方法比不使用散列鍵匹配更快嗎? – noledgeispower 2011-04-16 00:08:27

+0

實際上你並沒有真正使用散列鍵,因爲你只需要使用散列就可以創建一個獨特的正則表達式數組來再次測試。通過使用散列,您可以免費獲得獨特的測試,但必須花費時間從散列創建數組,然後才能使用它。 如果所需的測試是匹配顯式文件名,那測試就是'$ _ == $ key',那麼它可以被寫得更快,因爲'exists $ hExcludes {$ _}'並且避免了內部循環。 – 2011-04-16 08:19:45