在函数调用的同一行获取哈希键/值 [英] Get hash keys/values on same line as the function call

查看:28
本文介绍了在函数调用的同一行获取哈希键/值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是重现问题的代码:

sub hello { return (h => 1, n => 1); }
print join ", ", values hello();

我收到错误:

arg 1 to values 的类型必须是 hash(不是子程序入口)在 - 第 4 行,靠近 ");"执行 - 中止到期编译错误.

Type of arg 1 to values must be hash (not subroutine entry) at - line 4, near ");" Execution of - aborted due to compilation errors.

我知道我可以中断通话并打印两行:

I know I can break the call and the print on two lines:

sub hello { return (h => 1, n => 1); }
my %hash = hello();
print join ", ", values %hash;

但我不想那样做.有没有办法在一行中做到这一点,这样我就不必一直创建临时变量?

But I don't want to do that. Is there some way to do this in one line so that I don't have to create temporary variables all the time?

推荐答案

我没有看到在实际程序中的用处,但是是的,这是可能的.

I don't see the usefulness in a real program, but yes, it is possible.

print join ", ", values %{{hello()}};

说明:hello() 是一个列表;{hello()} 是一个哈希引用;%{{hello()}} 是一个哈希值.

Explanation: hello() is a list; {hello()} is a hash reference; %{{hello()}} is a hash.

这篇关于在函数调用的同一行获取哈希键/值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆