简体   繁体   中英

Perl, dereference array of references

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?

This stuff is tricky.

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

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

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