1

I have two bits of code that produce different output, and I am having great difficulty understanding why.

Code snippet 1:

my @args = $bighash{'arguments'}{'allocations'};
print "$args[0][1]";

Code snippet 2:

my @args = $bighash{'arguments'}{'allocations'}[0];
print "$args[1]";

In the first case, it is printing the value I expect. In the second case, it's not printing anything at all. Can anyone explain this?

2
  • 2
    There is no need to quote arguments to print. Commented Oct 27, 2011 at 22:56
  • 1
    this code seems wrong: As Eric pointed out, the thing in the hash would be a array reference, not an array. So it should be my $args = $bighash... ; print $args->[0]->[1] ; Try using Data::Dumper to print the whole thing, and take a look at perldoc.perl.org/perlreftut.html Commented Oct 28, 2011 at 8:42

3 Answers 3

2

The values stored in your hash are array references. You can work with the reference directly:

my $args = $bighash{'arguments'}{'allocations'};
print $$args[1];  # or $args->[1]

Or you can unpack the array into a new one:

my @args = @{ $bighash{'arguments'}{'allocations'} };
print $args[1];

More detail on the perlref and perldsc man pages.

Sign up to request clarification or add additional context in comments.

4 Comments

what does print $bighash{arguments}{allocations}[0][1]; print out?
Actually, I think I got it figured out. I am using your second solution without the [0].
Yep, sorry, I misread your question. The answer should be correct now.
What is the meaning of the double dollar sign?
2

Eric Strom's answer basically set's you right... but I'm surprised nobody has suggested using Data::Dumper to examine the data-structures you are working with. You can really see the difference between what you've done and Eric's correction. This might help set things straight for you.

Re: use warnings... you must get a warning when you try to print the non-existent element of the newly created array? I once got told never to ask anything online until I've used strict and warnings. That's maybe a bit extreme, but -w and Data::Dumper definitely help me :-)

1 Comment

Someone has suggested using Data::Dumper. They posted it as a comment.
0

In the code snippet 2, youre effectively taking the first element in your bighash ([0] is the first element) and putting that as the only element in an array. Then you ask for the second element, which does not exist.

If you remember to 'use warnings', this should yield one, since you're assigning a scalar as an array. I have not tested this myself, though.

3 Comments

Wow...that was not my intention at all. Does the assignment operator effectively act as an "insert" or "append" into a new array? How can I create a new array which is a reference to $bighash{'arguments'}{'allocations'}?
No warnings here, assigning a scalar to an array is fine, it just creates an array with one element.
Adam: You don't create an array which is a reference to.., you create a reference to an array. The reference itself is not an array.

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.