加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何使用散列作为Perl子例程的参数?

发布时间:2020-12-15 21:21:36 所属栏目:大数据 来源:网络整理
导读:我被要求修改一些现有代码以添加一些附加功能。我在Google上搜索过,似乎找不到答案。我有这样的事情… %first_hash = gen_first_hash();%second_hash = gen_second_hash();do_stuff_with_hashes(%first_hash,%second_hash);sub do_stuff_with_hashes{ my %f
我被要求修改一些现有代码以添加一些附加功能。我在Google上搜索过,似乎找不到答案。我有这样的事情…
%first_hash = gen_first_hash();
%second_hash = gen_second_hash();
do_stuff_with_hashes(%first_hash,%second_hash);

sub do_stuff_with_hashes
{
    my %first_hash = shift;
    my %second_hash = shift;

    # do stuff with the hashes
}

我收到以下错误:

Odd number of elements in hash assignment at ./gen.pl line 85.
Odd number of elements in hash assignment at ./gen.pl line 86.
Use of uninitialized value in concatenation (.) or string at ./gen.pl line 124.
Use of uninitialized value in concatenation (.) or string at ./gen.pl line 143.

第85行和第86行是子程序中的前两行,124和143是我访问哈希的位置。当我查看这些错误时,似乎表明我的哈希值未初始化。但是,我可以验证散列值是否具有值。为什么我得到这些错误?

解决方法

当您将它们传递到函数中时,哈希正被折叠成平面列表。所以,当你从函数的参数中移除一个值时,你只能得到一个值。你想做的是通过引用通过散列。
do_stuff_with_hashes(%first_hash,%second_hash);

但是,你必须使用哈希值作为参考。

my $first_hash  = shift;
my $second_hash = shift;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读