8

In the following Perl code, I would expect to be referencing an array reference inside an array

#!/usr/bin/perl

use strict;
use warnings;

my @a=([1,2],[3,4]);

my @b = @$a[0];

print $b[0];

However it doesn't seem to work. I would expect it to output 1.

@a is an array of references

@b is $a[1] dereferenced (I think)

So what's the problem?

1 Answer 1

11

This stuff is tricky.

@$a[0] is parsed as (@$a)[0], dereferencing the (undefined) scalar $a

You wanted to say @{$a[0]}.

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

2 Comments

there's so many quirks in Perl I'm still getting used to. thanks for your help
When in doubt, just add more braces :D

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.