perl里面参数和返回值都是数组吧,不过有很多默认写法,所以就算是数组也可以当作一个返回值来用.
$ cat a.pl @c=(3,2,1); $d=@c; print $d; $ perl a.pl 3 perl的默认东西很多,比如我变量没有用$a和$b,因为是保留的. 你定义函数的时候也没有参数列表不是么.参数列表就是@_,要手动提取. 由于有很多默认而且,这些默认都是因为好用才弄出来的.所以或许可以加快写的速度. 可读性的话,就两说了. 在 2011年7月6日 下午5:42,Dejian Zhao <[email protected]> 写道: > 呵呵,对方就是这个意思。人家用的是@array表示最终返回的数组(不是你子例程中的那个@arra),相当于你用的@result > > On 2011-7-1 9:58, Gary.jsz wrote: > > 您好! > > 如果使用 > 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 访问此网上论坛。 > > -- > 您收到此邮件是因为您订阅了 Google 网上论坛的"PerlChina Mongers 讨论组"论坛。 > 要向此网上论坛发帖,请发送电子邮件至 [email protected]。 > 要取消订阅此网上论坛,请发送电子邮件至 [email protected]。 > 若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。 > -- 您收到此邮件是因为您订阅了 Google 网上论坛的"PerlChina Mongers 讨论组"论坛。 要向此网上论坛发帖,请发送电子邮件至 [email protected]。 要取消订阅此网上论坛,请发送电子邮件至 [email protected]。 若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。
