user110084 user110084 - 13 days ago 4
Perl Question

Perl: slicing an array of hashes

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";