perl的:使用哈希内的阵列上推() [英] perl: using push() on an array inside a hash
问题描述
是否有可能使用Perl的推()功能的阵列上的哈希里面呢?
Is it possible to use Perl's push() function on an array inside a hash?
下面是我认为是我的工作在一个更大的计划的一部分违规。
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.
如果$域{$}标识符[0]是一个数组,那么为什么我不能用推语句元素添加到它?
If $domains{$identifier}[0] is an array, then why can I not use the push statement to add an element to it?
推荐答案
$ {域}标识符[0]
不是数组。
结果 $ {域}标识符[0]
是一个数组元素,标量。
结果 $ {域}标识符[0]
是一个数组的引用。
$domains{identifier}[0]
is not an array.
$domains{identifier}[0]
is an array element, a scalar.
$domains{identifier}[0]
is a reference to an array.
如果是
@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]);
参考文献:
- Mini-Tutorial: Dereferencing Syntax
- References quick reference
- perlref
- perlreftut
- perldsc
- perllol
这篇关于perl的:使用哈希内的阵列上推()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!