在函数调用的同一行获取哈希键/值

编程入门 行业动态 更新时间:2024-10-23 05:38:17
本文介绍了在函数调用的同一行获取哈希键/值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是重现问题的代码:

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.

更多推荐

在函数调用的同一行获取哈希键/值

本文发布于:2023-10-27 23:02:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534797.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   哈希键

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!