perl中这个变量的数据结构是什么?

发布于 2024-12-20 06:29:20 字数 207 浏览 2 评论 0原文

我是 Perl 新手,正在阅读用 Perl 编写的代码。一行内容如下:

$Map{$a}->{$b} = $c{$d};

我熟悉哈希看起来像 %samplehash 并通过 $samplehash{a}="b" 访问,

但是上面的行说明了什么实际上是地图吗?

I am new to perl and reading a code written in perl. A line reads like this:

$Map{$a}->{$b} = $c{$d};

I am familiar with hash looking like %samplehash and accessed as $samplehash{a}="b"

but what does the above line say about what is Map actually?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万劫不复 2024-12-27 06:29:20

给定这些变量:

my $a = "apples";
my $b = "pears";
my %c = ("bananas" => 2);
my $d = "bananas";
my %Map;

赋值

$Map{$a}->{$b} = $c{$d};

结果看起来像这样的哈希:

%Map = (
    "apples" => {
        "pears" => 2
    }
);

%Map 是一个哈希,在赋值之后通过自动激活包含哈希引用: 如果尚不存在,则自动创建内部哈希引用通过 Perl 通过访问 %Map 哈希中的元素 $Map{$a}->{$b} 来实现。

Given these variables:

my $a = "apples";
my $b = "pears";
my %c = ("bananas" => 2);
my $d = "bananas";
my %Map;

The assignment

$Map{$a}->{$b} = $c{$d};

Results in a hash looking like this:

%Map = (
    "apples" => {
        "pears" => 2
    }
);

%Map is a hash, which after the assignment contains a hash ref through autovivification: If not already there, the inner hash ref is automatically created by Perl by accessing the element $Map{$a}->{$b} in the %Map hash.

万劫不复 2024-12-27 06:29:20
$Map{$a}->{$b}

相当于

${ $Map{$a} }{$b}

$hash{$b}

使用哈希引用 $Map{$a} 而不是 %hash

请参阅 http://perlmonks.org/?node=References+quick+reference关于如何使用嵌套数据结构的一些容易记住的规则。

此外,在自动激活(默认情况下)的情况下,如果 $Map{$a} 以不存在或 undef 的形式启动,它将被隐式初始化为新的哈希引用。

$Map{$a}->{$b}

is equivalent to

${ $Map{$a} }{$b}

which is like

$hash{$b}

only using the hash reference $Map{$a} instead of %hash.

See http://perlmonks.org/?node=References+quick+reference for some easy to remember rules about how to use nested data structures.

Additionally, with autovivification on (which it is by default), if $Map{$a} starts as not existing or undef, it will be implicitly initialized to be a new hash reference.

翻身的咸鱼 2024-12-27 06:29:20

$Map 中键 $a 的值是关联数组的引用,该数组的键名称存储在 $b 中。

%Map = ( $a => { $b => $c{$d} }, ...)

the value for key $a in $Map is an reference of associate array which has a key name stored in $b.

%Map = ( $a => { $b => $c{$d} }, ...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文