无法储存数组作为哈希值 [英] Unable to store array as a hash value

查看:138
本文介绍了无法储存数组作为哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图存储阵列(未数组引用)哈希但它处理数组中的标量上下文的只有存储阵列中的 $散列方式> {键}

 使用数据::自卸车;
$ H-GT&{'A'} =('str_1','str_2');
打印自卸车$ H;

输出 $ VAR1 = {'A​​'=> str_2'};

为什么我不能存储在哈希键的数组,并访问数组元素为 $散列> {}键[指数]


解决方案

  $ H-GT&{'A'} = ['str_1','str_2'];

您只能存放标量散列值,和标量可以是简单的值或数组引用。

的perldoc 。

I'm trying to store an array (not array ref) in a hash but it is treating the array in scalar context and only storing the last value of array in the $hash->{key}.

use Data::Dumper;
$h->{'a'} = ( 'str_1', 'str_2' );
print Dumper $h;

Output: $VAR1 = { 'a' => 'str_2' };

Why can't I store an array in hash-Key and access the array elements as $hash->{key}[index]

解决方案

$h->{'a'} = [ 'str_1', 'str_2' ];

You can only store scalar as a hash value, and scalar can be simple value or array reference.

Check perldoc.

这篇关于无法储存数组作为哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆