简体   繁体   中英

Perl: How to access an array in a hash

I have a sample hash with an array inside, but it seems wrong the way I access the elements of the array. I do it this way:

%Hash_Object = (
  "Property1","value-1",
  "Property2",("value-2","value-3")
);

print $Hash_Object{Property2}[1];
#blank output!

It is supposed to print out "value-3", but it doesn't.

You do not have an array in your hash. You have a list. Keep the following in mind:

  • Lists are not the same as arrays in Perl
  • Lists are flat structures
  • Arrays are lists

If you put an array into a list, it will be treated as another list, and lists are flat:

(1, 2, (3, 4, 5), (6, (7)))

is equal to

(1, 2, 3, 4, 5, 6, 7)

If you want to build more complex data structures, you need to use references . There are two ways to make a reference. You can either reference a variable by using \\ like this

my @foo = qw(a b c);
my $ref = \@foo;

or by constructing it directly as an anonymous reference that you then assign to a variable.

my $ref = [ qw(a b c) ];
my $ref2 = [ 1, 2, 3 ];

To make a hash reference, use curly braces {} .

my $ref = { a => 1, b => 2 };

References are scalar values, so they are themselves just a single flat value. That's why you need to dereference them in order to get to the value that's inside of them (really it's not inside, it's referenced ).

%Hash_Object = (
  "Property1","value-1",
  "Property2",["value-2","value-3"]
);
$Hash_Object{Property2}[1];
$Hash_Object{Property2}->[1]; # or like this with ->

You already knew how to do that. You can also use the -> operator in front of every new dereference. Some people find that clearer to read.

For more information, see perlreftut and perlref as well as Mike Friedman's excellent blog post about lists and arrays .


Your example is not very well written code. Here are some improvement.

  • variable names should be lower-case
  • use the fat comma => for hash assignments
  • you don't need double quotes "" if you're not interpolating
  • always put commas after the final element
  • don't name things for what type they are, name them for what they represent
  • a hash is not an object
  • you need to use my when declaring a new variable

my %example = (
    Property1 => 'value-1',
    Property2 => [
        'value-2',
        'value-3',
    ],
);

Always use use warnings; and use strict; in top of the program.

If you use this it display the following errors

Odd number of elements in hash assignment at array.pl line 3.
Can't use string ("value-2") as an ARRAY ref while "strict refs" in use at array.pl line 8

In perl, where list are flatten together .

so the first error is

Odd number of elements in hash assignment at array.pl line 3

Hashes must has pairs of keys and value. So the elements of the hash should not be an odd number.

Your code should be

use warnings;
use strict;
my %Hash_Object = (
  "Property1"=>["value-1"],
  "Property2"=>["value-2","value-3"]
);

print $Hash_Object{Property2}[1];

Array values should be in square brackets, thanks simbabque

%Hash_Object = (
  "Property1","value-1",
  "Property2",["value-2","value-3"]
);

print $Hash_Object{Property2}[1];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM