好吧,所以我想採取一個散列,如果在數組中的任何字符串包含密鑰(不值實際的密鑰名稱)在哈希丟棄它。否則打印出字符串。這個問題與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
);
你只需要說出你的意思是什麼「它打破」?一個提示:你正在替代其值在正則表達式中包含元字符的變量,即「.nfs」。您可能需要編寫/ \ Q $ key \ E /以提供報價。 – Ingo 2011-04-15 23:32:06
-1你發佈的代碼不可能「工作」,因爲它甚至沒有編譯。您應該確保發佈與您運行的代碼完全相同的代碼! – tadmc 2011-04-16 00:06:37