如何取消引用已传递给子例程的 Perl 哈希引用?

发布于 2024-08-21 21:46:14 字数 284 浏览 2 评论 0原文

我仍在尝试解决我的哈希取消引用问题。我当前的问题是我现在将 hashref 传递给子组件,并且我想在该子组件中取消引用它。但我没有找到正确的方法/语法来做到这一点。在 sub 中,我想迭代哈希键,但 hashref 的语法与哈希不同,我知道该怎么做。

所以我想要的是这样做:

sub foo {
    %parms = @_;
    foreach $keys (key %parms) { # do something };
}

但传入的是 hashref 而不是哈希。

I'm still trying to sort out my hash dereferencing. My current problem is I am now passing a hashref to a sub, and I want to dereference it within that sub. But I'm not finding the correct method/syntax to do it. Within the sub, I want to iterate the hash keys, but the syntax for a hashref is not the same as a hash, which I know how to do.

So what I want is to do this:

sub foo {
    %parms = @_;
    foreach $keys (key %parms) { # do something };
}

but with a hashref being passed in instead of a hash.

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

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

发布评论

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

评论(5

如果没有你 2024-08-28 21:46:14

我目前还没有实际测试代码,但是徒手编写你会想要做这样的事情:

sub foo {
    $parms = shift;
    foreach my $key (keys %$parms) { # do something };
}

I havn't actually tested the code at this time, but writing freehand you'll want to do something like this:

sub foo {
    $parms = shift;
    foreach my $key (keys %$parms) { # do something };
}
旧情勿念 2024-08-28 21:46:14

以下是取消引用传递给子节点的哈希引用的一种方法:

use warnings;
use strict;

my %pars = (a=>1, b=>2);
foo(\%pars);
sub foo {
    my ($href) = @_;
    foreach my $keys (keys %{$href}) { print "$keys\n" }
}

__END__
a
b

另请参阅引用快速参考perlreftut

Here is one way to dereference a hash ref passed to a sub:

use warnings;
use strict;

my %pars = (a=>1, b=>2);
foo(\%pars);
sub foo {
    my ($href) = @_;
    foreach my $keys (keys %{$href}) { print "$keys\n" }
}

__END__
a
b

See also References quick reference and perlreftut

千纸鹤 2024-08-28 21:46:14

sub foo
{
    my $params = $_[0];
    my %hash = %$params;
        foreach $keys (keys %hash)
        {
         print $keys;
        }
}

my $hash_ref = {name => 'Becky', age => 23};

foo($hash_ref);

这里还有一个关于引用的很好的介绍。


sub foo
{
    my $params = $_[0];
    my %hash = %$params;
        foreach $keys (keys %hash)
        {
         print $keys;
        }
}

my $hash_ref = {name => 'Becky', age => 23};

foo($hash_ref);

Also a good intro about references is here.

花海 2024-08-28 21:46:14
#!/usr/bin/perl
use strict;

my %params = (
    date => '2010-02-17',
    time => '1610',
);

foo(\%params);

sub foo {
    my ($params) = @_;
    foreach my $key (keys %$params) {
        # Do something
        print "KEY: $key VALUE: $params{$key}\n";
    };
}
#!/usr/bin/perl
use strict;

my %params = (
    date => '2010-02-17',
    time => '1610',
);

foo(\%params);

sub foo {
    my ($params) = @_;
    foreach my $key (keys %$params) {
        # Do something
        print "KEY: $key VALUE: $params{$key}\n";
    };
}
抽个烟儿 2024-08-28 21:46:14

取消引用原始引用,但将其用作另一个/其副本,这也是很常见或更常见的。

sub foo {
    my ($parmsHashRef , $parmsArrRef) = @_;

    # for hashes,   \%$parmsHashRef - would not work (seems would be a "reference" on another reference already)
    my $newDetachedHashRef = {%$parmsHashRef} # or just = {$parmsHashRef}
    # for arrays ,  \@$parmsArrRef - would not work
    my $newDetachedArrRef = [@$parmsArrRef] # or just = [$parmsArrRef]
}

Dereference the original reference, but use it as another one / its copy, what is also very common or more common.

sub foo {
    my ($parmsHashRef , $parmsArrRef) = @_;

    # for hashes,   \%$parmsHashRef - would not work (seems would be a "reference" on another reference already)
    my $newDetachedHashRef = {%$parmsHashRef} # or just = {$parmsHashRef}
    # for arrays ,  \@$parmsArrRef - would not work
    my $newDetachedArrRef = [@$parmsArrRef] # or just = [$parmsArrRef]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文