0

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.

4
  • 4
    You don't have an array in your hash. You have a list. I'm looking for a duplicate with a good explanation. In the meantime, you need to use [] inside the hash assignment. Commented Oct 27, 2016 at 7:51
  • 1
    relevant, but not duplicate: stackoverflow.com/q/40044519/1331451 Commented Oct 27, 2016 at 7:53
  • 1
    %Hash_Object = ( "Property1","value-1", "Property2",["value-2","value-3"] ); Commented Oct 27, 2016 at 7:54
  • 1
    I bet there are ten questions like this one, but I cannot find them. Answering instead. Commented Oct 27, 2016 at 7:56

3 Answers 3

4

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',
    ],
);
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

I didn't even see the odd number. Well spotted.
The last assumption that each element should be an array reference is not correct. You don't know if OP wants that. They just showed that there is an array in Property2.
@simbabque Thank you for your comment. Just now I knew it.
0

Array values should be in square brackets, thanks simbabque

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

print $Hash_Object{Property2}[1];

1 Comment

"Array values should be in square brackets" - well, only when you want a reference to that array (which you do in this case, because you're building a complex data structure).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.