简体   繁体   English

Perl数组和哈希引用处理

[英]Perl Array and Hash references Handling

my problem is that i'm getting no Output at the end of Script.. All the prints while parsing are ok, but arrays at end are empty. 我的问题是我在脚本结束时没有输出..解析时的所有打印都没问题,但结尾处的数组是空的。

What i'm doing wrong? 我做错了什么? Isnt this the way to handle the refs? 这不是处理裁判的方法吗?

thx 4 response 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>";

Sample Input: 样本输入:

    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

It's quite hard to figure it out without any input data, because it means that we can't run a copy of the script on our own machines! 没有任何输入数据很难弄明白,因为这意味着我们无法在自己的机器上运行脚本的副本! It is generally pretty useful if your sample code is self-contained ! 如果您的示例代码是自包含的,通常非常有用!

That said, I think your problem stems from doing this kind of thing: 也就是说,我认为你的问题源于做这样的事情:

my $branch_ref = $branches{$1};
%tmp_branch = %$branch_ref;

The second line does a shallow copy of the hash, so %tmp_branch is no longer the same hash as the one referenced by $branches{$1} . 第二行执行散列的浅表副本 ,因此%tmp_branch不再是与$branches{$1}引用的散列相同的散列。 When you add data to the %tmp_branch hash, you are not adding data to the $branches{$1} hash. 将数据添加到%tmp_branch哈希时,不会将数据添加到$branches{$1}哈希。

@tmp_array suffers similarly. @tmp_array同样受苦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM