perl 从子例程打印数组

发布于 2024-09-08 05:16:13 字数 302 浏览 5 评论 0原文

#! /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 技术交流群。

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

发布评论

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

评论(4

清风挽心 2024-09-15 05:16:14

您正在返回一个引用,并且不会在任何地方取消引用它。

foreach $cluster (@{getClusters()})

或者

return @clusters;

要么应该修复它(效果略有不同),第一个是首选(你的数组有点大)。

您可以使用非引用数组返回有限数量的元素,通常是为了多重返回(因此,通常限制为 2 或 3 个已知长度的数组)。

You are returning a reference, and not dereferencing it anywhere.

foreach $cluster (@{getClusters()})

OR

return @clusters;

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).

月隐月明月朦胧 2024-09-15 05:16:14

如果您在 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.

怎樣才叫好 2024-09-15 05:16:14

Perl 解决方案

#!/usr/local/bin/perl
use strict;
use warnings;

main();

sub main{
   {
      local $"    =  "\n";
      print "@{getClusters()}";
   }
}  

sub getClusters{
   my @tArray  =  `/qbo/bin/getclusters|grep -v 'qboc33'`;
   chomp @tArray;
   return \@tArray;
}

注意

  1. 您不需要 foreach 循环进行调试,您只需重置 $" 运算符即可按照您喜欢的方式分隔数组元素(例如, 或我在上面的代码中设置它的方式 \n
  2. 返回数组引用是一个优点,不要发回完整的数组。 (干得好)
  3. 使用严格/警告,特别是在调试时
  4. 尝试避免使用 `` 进行系统调用

Perl Solution

#!/usr/local/bin/perl
use strict;
use warnings;

main();

sub main{
   {
      local 
quot;    =  "\n";
      print "@{getClusters()}";
   }
}  

sub getClusters{
   my @tArray  =  `/qbo/bin/getclusters|grep -v 'qboc33'`;
   chomp @tArray;
   return \@tArray;
}

Notice

  1. You don't need a 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).
  2. Returning an array ref is a plus, don't send back the full array (good job)
  3. use strict/warnings, especially when debugging
  4. try to avoid system calls using ``
萌吟 2024-09-15 05:16:14

为了方便起见,您可以先接收返回值,然后像这样打印

use strict;
use warning;
my $cluster_array = getClusters();
 my @cluster_return = @{$cluster_array};
foreach my $cluster(@cluster_return){
 print"$cluster\n";
}

To make it easy, you can first receive the return value and then print it like

use strict;
use warning;
my $cluster_array = getClusters();
 my @cluster_return = @{$cluster_array};
foreach my $cluster(@cluster_return){
 print"$cluster\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文