如何在 Perl 中引用特定的哈希值?

发布于 2024-08-26 03:13:38 字数 248 浏览 2 评论 0原文

如何创建对特定哈希键中的值的引用。我尝试了以下方法,但 $$foo 是空的。非常感谢任何帮助。

$hash->{1} = "one";
$hash->{2} = "two";
$hash->{3} = "three";

$foo = \${$hash->{1}};
$hash->{1} = "ONE";

#I want "MONEY: ONE";
print "MONEY: $$foo\n";

How do I create a reference to the value in a specific hash key. I tried the following but $$foo is empty. Any help is much appreciated.

$hash->{1} = "one";
$hash->{2} = "two";
$hash->{3} = "three";

$foo = \${$hash->{1}};
$hash->{1} = "ONE";

#I want "MONEY: ONE";
print "MONEY: $foo\n";

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

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

发布评论

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

评论(3

情仇皆在手 2024-09-02 03:13:38
use strict;
use warnings;
my $hash;

$hash->{1} = "one";
$hash->{2} = "two";
$hash->{3} = "three";

my $foo = \$hash->{1};
$hash->{1} = "ONE";
print "MONEY: $foo\n";
use strict;
use warnings;
my $hash;

$hash->{1} = "one";
$hash->{2} = "two";
$hash->{3} = "three";

my $foo = \$hash->{1};
$hash->{1} = "ONE";
print "MONEY: $foo\n";
画离情绘悲伤 2024-09-02 03:13:38

打开严格和警告,您会得到一些关于出了什么问题的线索。

use strict;
use warnings;

my $hash = { a => 1, b => 2, c => 3 };
my $a = \$hash{a};
my $b = \$hash->{b};

print "$a $b\n";

一般来说,如果你想用切片 或获取 refs 进行操作,你必须使用旧式的、堆积的 sigil 语法来获得你想要的。如果您不记得堆积的印记语法,您可能会发现参考快速参考很方便细节。

更新

正如murugaperumal指出的,你可以做my $foo = \$hash->{a};我可以发誓我尝试过但它不起作用(令我惊讶的是)。我会把这归因于疲劳让我变得更加愚蠢。

Turn on strict and warnings and you'll get some clues as to what's going wrong.

use strict;
use warnings;

my $hash = { a => 1, b => 2, c => 3 };
my $a = \$hash{a};
my $b = \$hash->{b};

print "$a $b\n";

In general, if you want to do things with slices or taking refs, you've got to use the old style, piled sigil syntax to get what you want. You may find the References Quick Reference handy, if you don't recall the piled sigil syntax details.

update

As murugaperumal points out, you can do my $foo = \$hash->{a}; I could swear I tried that and it didn't work (to my surprise). I'll chalk it up to being fatigue making me extra foolish.

老子叫无熙 2024-09-02 03:13:38

一个经典,但在你以两种方式说明之前,这些例子似乎并不完整

use strict;
use warnings;

my $hash = { abc => 123 };
print $hash->{abc} . "\n"; # 123 , of course

my $ref = \$hash->{abc};
print $ref . "\n"; # 123 , of course

$hash->{abc} = 456;
print $ref . "\n"; # 456 , change in the hash reflects in the $ref

$ref = 789;
print $hash->{abc} . "\n"; # 789 , change in the $ref also reflects in the hash

PS:尽管这是一个古老的话题,但我决定扔掉我的两分钱,因为我看到我以前访问过同样的问题

a classic, and yet the examples don't seem to be complete until you illustrate it both ways

use strict;
use warnings;

my $hash = { abc => 123 };
print $hash->{abc} . "\n"; # 123 , of course

my $ref = \$hash->{abc};
print $ref . "\n"; # 123 , of course

$hash->{abc} = 456;
print $ref . "\n"; # 456 , change in the hash reflects in the $ref

$ref = 789;
print $hash->{abc} . "\n"; # 789 , change in the $ref also reflects in the hash

PS: despite being an old topic, I decided to throw my two cents, since I saw I've visited this same question before

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