开发者

Perl, dereference array of references

开发者 https://www.devze.com 2023-01-02 18:17 出处:网络
In the following Perl code, I would expect to be referencing an array reference inside an array #!/usr/bin/perl

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

#!/usr/bin/perl

use strict;开发者_StackOverflow
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]}.

0

精彩评论

暂无评论...
验证码 换一张
取 消