perl 从子例程打印数组
#! /usr/local/bin/perl
sub getClusters
{
my @clusters = `/qbo/bin/getclusters|grep -v 'qboc33'`;
chomp(@clusters);
return \@clusters;
}
嗯,好吧..我如何获取这个数组来打印,因为......
foreach $cluster (getClusters())
{ print $cluster."\n"; }
似乎不起作用。 谢谢。
#! /usr/local/bin/perl
sub getClusters
{
my @clusters = `/qbo/bin/getclusters|grep -v 'qboc33'`;
chomp(@clusters);
return \@clusters;
}
ummm okay .. how do I get at this array to print since ...
foreach $cluster (getClusters())
{ print $cluster."\n"; }
doesn't seem to work.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在返回一个引用,并且不会在任何地方取消引用它。
或者
要么应该修复它(效果略有不同),第一个是首选(你的数组有点大)。
您可以使用非引用数组返回有限数量的元素,通常是为了多重返回(因此,通常限制为 2 或 3 个已知长度的数组)。
You are returning a reference, and not dereferencing it anywhere.
OR
Either should fix it (with slightly different effects), with the first one being preferred (your array is kind of big).
You'd use the non-referenced array return for limited number of elements, usually for the purpose of multi-return (thus, usually, limited to 2 or 3, known-length arrays).
如果您在
use strict 下运行程序;使用警告;
,它会告诉你失败的原因。正如阿马丹所说,您需要取消引用您返回的引用。If you ran your program under
use strict; use warnings;
, it would have told you why it failed. As Amadan said, you need to dereference the reference you return.Perl 解决方案
注意
foreach
循环进行调试,您只需重置$"
运算符即可按照您喜欢的方式分隔数组元素(例如,、
、
或我在上面的代码中设置它的方式\n
``
进行系统调用Perl Solution
Notice
foreach
loop for debugging, you can just reset the$"
operator however to separate array elements however you like (eg,,
,
, or how I set it in the code above\n
).``
为了方便起见,您可以先接收返回值,然后像这样打印
To make it easy, you can first receive the return value and then print it like