简体   繁体   English

perl:在哈希内的数组上使用push()

[英]perl: using push() on an array inside a hash

Is it possible to use Perl's push() function on an array inside a hash? 是否可以在哈希内的数组上使用Perl的push()函数?

Below is what I believe to be the offending part of a larger program that I am working on. 以下是我认为是我正在研究的较大程序的有问题的部分。

my %domains = ();
open (TABLE, "placeholder.foo") || die "cannot read domtblout file\n";
while ($line = <TABLE>)
{
    if (!($line =~ /^#/))                                                                 
    {
            @split_line = split(/\t/, $line);                                              # splits on tabs - some entries contain whitespace
        if ($split_line[13] >= $domain_cutoff)
        {
            push($domains{$split_line[0]}[0], $split_line[19]);                    # adds "env from" coordinate to array
            push($domains{$split_line[0]}[1], $split_line[20]);                    # adds "env to" coordinate to array
            # %domains is a hash, but $domains{identifier}[0] and $domains{$identifier}[1] are both arrays
            # this way, all domains from one sequence are stored with the same hash key, but can easily be processed iteratively
        }

    }

}

Later I try to interact with these arrays using 后来我尝试使用

for ($i = 0, $i <= $domains{$identifier}[0], $i++)
        {
            $from = $domains{$identifier}[0][$i];
            $to = $domains{$identifier}[1][$i];
            $length = ($to - $from);
            $tmp_seq =~ /.{$from}(.{$length})/;
            print("$header"."$1");
        }

but it appears as if the arrays I created are empty. 但似乎我创建的数组为空。

If $domains{$identifier}[0] is an array, then why can I not use the push statement to add an element to it? 如果$ domains {$ identifier} [0]是一个数组,那么为什么不能使用push语句向其中添加元素?

$domains{identifier}[0] is not an array. $domains{identifier}[0]不是数组。
$domains{identifier}[0] is an array element, a scalar. $domains{identifier}[0]是一个数组元素,一个标量。
$domains{identifier}[0] is a reference to an array. $domains{identifier}[0]是对数组的引用。

If it's 如果它是

@array

when you have an array, it's 当你有一个数组,它是

@{ ... }

when you have a reference to an array, so 当您引用数组时,

push(@{ $domains{ $split_line[0] }[0] }, $split_line[19]);

References: 参考文献:

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

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