是否可以将数组的引用分配为key : value
perl 中哈希表对中的值?
问问题
24521 次
4 回答
29
是的。使用反斜杠创建对数组的引用:
$hash{key} = \@array;
请注意,这将链接到实际数组,因此如果您执行以下更改:
$array[0] = "foo";
这也意味着$hash{key}[0]
设置为"foo"
。
如果这不是您想要的,您可以使用匿名数组引用来复制这些值[ ... ]
:
$hash{key} = [ @array ];
此外,您不必通过数组来执行此操作。您可以简单地直接分配:
$hash{key} = [ qw(foo bar baz) ];
于 2012-12-20T04:15:54.243 回答
10
是的。有关访问此类数据结构的一些基本规则,请参阅http://perlmonks.org/?node=References+quick+reference ,但要创建它,只需执行以下操作之一:
%hash = ( 'somekey' => \@arrayvalue );
$hash{'somekey'} = \@arrayvalue;
%hash = ( 'somekey' => [ ... ] );
于 2012-12-20T04:16:02.270 回答
-1
%hash = ( 'somekey' => \@arrayvalue );
$hash{'somekey'} = \@arrayvalue;
It is right. We can assign either hash or hash reference format.
于 2019-07-13T18:21:26.730 回答
-1
use Data::Dumper; @name=('5/17',
'5/17','5/17','5/17','5/17','5/17','5/17','5/17'); @status_flags=('U
H L','U C','U H L','U C','U C','U H L','U C', 'U H L');
@ip_address=('192.168.0.11','192.168.0.2','192.168.0.13','192.168.0.0','192.168.0.3','192.168.0.12','192.168.0.4','192.168.0.14'); @dp_id=('0','0','0','0','0','0','0','0');
@ip_prefix_length=('32','32','32','24', '32', '32','32','32');
for ($value=0;$value<=5;$value++) {
$keyvals{'Response'}{'brocade-extension-ip-route'}{'extension-ip-route'}={'name'=>"$name[$value]"};
$keyvals{'Response'}{'brocade-extension-ip-route'}{'extension-ip-route'}={'dp-id'=>"$dp_id[$value]"};
$keyvals{'Response'}{'brocade-extension-ip-route'}{'extension-ip-route'}={'ip-address'=>"$ip_address[$value]"};
$keyvals{'Response'}{'brocade-extension-ip-route'}{'extension-ip-route'}={'ip-prefix-length'=>"$ip_prefix_length[$value]"};
$keyvals{'Response'}{'brocade-extension-ip-route'}{'extension-ip-route'}={'ip-gateway'=>'*'};
}
print Dumper \%keyvals;
Each array value assign into hash value. $var1= {
'Response' => {
'extension-ip-route' => {
'status-flags' => 'U H L '
,
'ip-gateway' => '*',
'name' => '0/2',
'ip-address' => '192.168.20.11',
'dp-id' => '0',
'ip-prefix-length'=>'32'
}
}
};
于 2017-07-06T16:53:17.600 回答