Perl:切片哈希 [英] Perl: slicing an array of hashes

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

问题描述

以下代码的输出始终为空.不知道我在做什么错,将不胜感激.如何获取散列数组中特定散列中的键的值?

The output of the code below is always empty. Not sure what I am doing wrong and would appreciate any help. How do I get to the values of a key in a specific hash in an array of hashes?

use strict;
use warnings;

my %dot1 = ('a'=>1,'b'=>2);
my %dot2 = ('a'=>3,'b'=>4);
my %dot3 = ('a'=>5,'b'=>6);
my %dot4 = ('a'=>7,'b'=>8);

my @array = (%dot1,%dot2,%dot3,%dot4);

my %x = $array[2];
my $y = $x->{'a'};

print "$y \n";

推荐答案

如果要使用散列引用数组,则需要这样明确地说明.

If you want an array of hash references, you need to say so explicitly.

my @array = (\%dot1, \%dot2, \%dot3, \%dot4);
my %x = %{$array[2]};
my $y = $x{a};
print "$y\n";

这篇关于Perl:切片哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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