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',
],
);
[]inside the hash assignment.%Hash_Object = ( "Property1","value-1", "Property2",["value-2","value-3"] );