[英]Perl Array and Hash references Handling
我的問題是我在腳本結束時沒有輸出..解析時的所有打印都沒問題,但結尾處的數組是空的。
我做錯了什么? 這不是處理裁判的方法嗎?
thx 4回應
my %branches = ();
print "<pre>";
my %tmp_branch;
while (defined($_ = shift @bugs)) {
my $bug_id = $_->id;
my $bug_product = $_->product;
my $content = $browser->get("http://****?ticket=".$bug_id);
$content = $content->decoded_content;
my @rows = split /\n/, $content;
my $trigger = 0;
while (defined($_ = shift @rows)) {
chomp;
if ($_ eq "") {
$trigger = 0;
}
elsif (/Branch: (.*)/) {
if (exists $branches{$1}) {
my $branch_ref = $branches{$1};
%tmp_branch = %$branch_ref;
print "existing Branch: $1\n";
} else {
my %new_branch = ();
my @sources = ();
my @wfs = ();
my @methods = ();
$new_branch{'sources'} = \@sources;
$new_branch{'methods'} = \@methods;
$new_branch{'wfs'} = \@wfs;
$branches{$1} = \%new_branch;
%tmp_branch = %new_branch;
print "new Branch: $1\n";
}
}
elsif (/Sourcen.*:/) {
$trigger = "sources";
}
elsif (/geaenderte Methoden.*:/) {
$trigger = "methods";
}
elsif (/geaenderte Workflows.*:/) {
$trigger = "wfs";
}
elsif ($trigger && $_ ne "") {
my $tmp_array_ref = $tmp_branch{$trigger};
my @tmp_array = @$tmp_array_ref;
push @tmp_array, $_;
print "find $trigger: $_\n";
}
}
}
print "\n\n\n";
while (my ($k,$v)=each %branches){
my $branch_ref = $v;
my %tmp_branch = %$branch_ref;
my $sources_ref = $tmp_branch{'sources'};
my @sources = @$sources_ref;
my $methods_ref = $tmp_branch{'methods'};
my @methods = @$methods_ref;
my $wfs_ref = $tmp_branch{'wfs'};
my @wfs = @$wfs_ref;
print "Branch: $k\nSources:\n";
print @sources;
print "\nMethods:\n";
print @methods;
print "\nWorkflows:\n";
print @wfs;
print "\n";
}
print "</pre>";
樣本輸入:
Kontext Auswertung fuer Ticket: #12345 (xxxxSomeTextxxx)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HINWEIS: xxxxSomeTextxxx
Branch: HEAD
~~~~~~~
Sourcen (4):
IamArow
IamArow2
IamArow3
IamArow4
geaenderte Methoden (1):
IamArow
geaenderte Workflows (2):
IamArow
IamArow2
沒有任何輸入數據很難弄明白,因為這意味着我們無法在自己的機器上運行腳本的副本! 如果您的示例代碼是自包含的,通常非常有用!
也就是說,我認為你的問題源於做這樣的事情:
my $branch_ref = $branches{$1};
%tmp_branch = %$branch_ref;
第二行執行散列的淺表副本 ,因此%tmp_branch
不再是與$branches{$1}
引用的散列相同的散列。 將數據添加到%tmp_branch
哈希時,不會將數據添加到$branches{$1}
哈希。
@tmp_array
同樣受苦。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.