您好! 

如果使用
print $_ foreach @arra是不行的,因为这个数组变量是在子例程里面的。
但是,如果使用:
my @result = output();
print $_ foreach @result;
就会把子例程里面所有的返回值都打印出来了。


不过,如果把@arra定义为全局变量,应该就解决这个问题了。



======= 2011-07-01 09:47 李�� 您在来信中写到:Re: [PerlChina] 子例程问题,请教 ======= 




hi, try this: 


print $_ foreach @array;



2011/7/1 Gary.jsz <[email protected]>

truncatei,您好! 


谢谢!

如果返回一个数组该怎么遍历打印?
比如:
sub output{
    my @arra = (a, b, c, d, e, f, g);

    my $host_name = 12345;
    my $kernel = "2.6.35";
    my $version = "Redhat enterprise linux";
    #print "$host_name\n $kernel\n $version\n";

    return (@arra, $host_name, $kernel, $version);
} 
因为数据的元素个数未知,从文件中获取的。





======= 2011-06-30 18:46 truncatei 您在来信中写到:Re: [PerlChina] 子例程问题,请教 ======= 

做法有很多种,比如:
sub output{
    my $host_name = 12345;
    my $kernel = "2.6.35";
    my $version = "Redhat enterprise linux";
    #print "$host_name\n $kernel\n $version\n";
    return ($host_name, $kernel, $version);
}
my @result = output();
print $result[1];

----
Gary.jsz


-- 
您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
要向此网上论坛发帖,请发送电子邮件至 [email protected]。
要取消订阅此网上论坛,请发送电子邮件至 [email protected]。
若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。




-- 

+++++++_++++++++
李 镇
DeNA China 智能手机事业部
(86-21-6267-6611-137)


大家一起来玩[梦宝谷]吧!
http://mobage.com.cn


++++++ *-* +++++++


-- 
您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
要向此网上论坛发帖,请发送电子邮件至 [email protected]。
要取消订阅此网上论坛,请发送电子邮件至 [email protected]。
若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。


= = = = = = = = = = = = = = = = = = = = 


----
Gary.jsz

-- 
您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
要向此网上论坛发帖,请发送电子邮件至 [email protected]。
要取消订阅此网上论坛,请发送电子邮件至 [email protected]。
若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。

回复