我想要做的是檢索一個鍵值對中的一個哈希鍵,因爲我從我正在閱讀的文件中獲得的是一個值。Perl - 如何知道值的哈希鍵?
的代碼產生這樣的:
12345
welcome.html
這部分的代碼是:
my %bugs;
my $bug;
open(FH, '-|', "lynx -dump '$queryurl'") or die "Could not lynx $queryurl: $!";
while (<FH>)
{
if (/<bz:id[^>]*>([^<]*)</)
{
$bug = $1;
}
if (/<bz:url[^>]*>([^<]*)</)
{
my $url = $1;
$bugs{$url} = $bug;
$bug = undef;
}
}
close(FH);
# for debugging purposes
foreach my $bug (keys %bugs)
{
print "$bugs{$bug} $bug\n";
}
exit;
然後,其他地方在一個名爲bad.txt
我得到的輸出喜歡文件:
Documents that failed: daerror 6 0 6 welcome.html
讀取該文件中的代碼是:
my $badfile = "$dir/bad.txt";
open(FH, "<$badfile") || die "Can not open $badfile: $!";
# ignore first line
<FH>;
while (<FH>)
{
chomp;
if (!/^([^ ]+) [^ ]+ [^ ]+ [^ ]+ ([^ ]+) [^ ]+$/)
{
die "Invalid line $_ in $badfile\n";
}
my $type = $1;
my $testdoc = $2;
}
,但我已經有使用正則表達式從這個提取的文件名。
[+] +也可以寫成\ S +您也可以使用'm'並在一行中設置$ type和$ testdoc。 my($ type,$ testdoc)= $ _ =〜m/^(\ S +)\ S + \ S + \ S +(\ S +)\ S + $ /除非定義了$ type && defined $ testdoc;',否則在「$ badfiles \ n中無效行$ _」。 – mrk