免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 14961 | 回复: 10
打印 上一主题 下一主题

函数在返回数组的时候遇到了问题。。。大家都来看看:) [复制链接]

论坛徽章:
0
1 [报告]
发表于 2010-08-04 15:30 |显示全部楼层
回复 1# py


    :emn1: 因为返回的是一个参数列表,而不是一个array.
  1. foreach my $var qw/1 2 3/ {

  2.     print "$var\n";

  3. }


  4. my $one = shift ( qw/1 2 3/);
复制代码

论坛徽章:
0
2 [报告]
发表于 2010-08-05 11:21 |显示全部楼层
  1. A "return" statement may be used to exit a subroutine, optionally specifying the returned value, which
  2.        will be evaluated in the appropriate context (list, scalar, or void) depending on the context of the sub-
  3.        routine call.  If you specify no return value, the subroutine returns an empty list in list context, the
  4.        undefined value in scalar context, or nothing in void context.  If you return one or more aggregates
  5.        (arrays and hashes), these will be flattened together into one large indistinguishable list.
复制代码
因为函数返回的是什么决定于调用该函数的环境。也就是说:
用$aa=$fun()的时候,这个是标量环境,那么函数返回的是数组或者列表处于标量环境的值,而不是返回这整个数组,或列表,然后再求值。

论坛徽章:
0
3 [报告]
发表于 2010-08-05 15:44 |显示全部楼层
黑色阳光_cu 是对的。编译不通过是因为:
shift(func()); 失败是因为shift的参数只能是array.而不是一个表达式, 而func()是一个表达式。


不是返回参数能不能修改的原因。下面例子可以解释:
  1. foreach my $tt(qw(1 3 5)){
  2. print $tt."\n";
  3. #$tt=1;
  4. }
复制代码
去掉注释就运行失败,因为qw是value,是只读的。
但函数返回值并不是只读的。看下面例子:
  1. sub function {
  2.     my @temp = qw/a b c/;
  3.     return @temp;
  4. }
  5. foreach my $var(function) {
  6.     print "$var\n";
  7.     $var=1;
  8. }
复制代码

论坛徽章:
0
4 [报告]
发表于 2010-08-05 16:08 |显示全部楼层
:emn1:不应该说表达式  应该说函数调用 o(∩_∩)o...

论坛徽章:
0
5 [报告]
发表于 2010-08-05 17:15 |显示全部楼层
因为作为参数传递的是临时变量。

论坛徽章:
0
6 [报告]
发表于 2010-08-06 09:01 |显示全部楼层
这里要说到一个机制,记得数组@aa=(1..1000000000)这样会占用很大的存储空间吗?
如果我们用for(1..100000000000)却不存在这问题。这是因为:
but you should be aware that the ".." operator creates an array of all integers in the range.  This can
       take a lot of memory for large ranges.  Instead use:

           @results = ();
           for ($i=5; $i < 500_005; $i++) {
               push(@results, some_func($i));
           }

       This situation has been fixed in Perl5.005. Use of ".." in a "for" loop will iterate over the range, with-
       out creating the entire range.

           for my $i (5 .. 500_005) {
               push(@results, some_func($i));
           }

       will not create a list of 500,000 integers.


Perl在5.005.版本之后对for做了优化。所以只占用一个地址位。


而for (qw(1 2 3 4)){..}这个之所以会占用两个地址,是因为perl的内存回收机制。

论坛徽章:
0
7 [报告]
发表于 2010-08-06 09:20 |显示全部楼层
在for语句中不算,但拿出来赋值还是算

论坛徽章:
0
8 [报告]
发表于 2010-08-06 09:21 |显示全部楼层
这里只是因为PERL针对FOR语句的优化

论坛徽章:
0
9 [报告]
发表于 2010-08-06 09:54 |显示全部楼层
本帖最后由 toniz 于 2010-08-06 10:04 编辑

我也来提个问题,看下面的代码:
  1. for my $aa(qw(1 2 3 4)){
  2. #        $aa=1234;
  3.         ${\$aa}=1234;
  4. }
复制代码
这样运行正常。去掉第一句的注释,就报错。。

$aa是只读的,而\$aa指向的变量却是可写的。
也就是说\$aa并不是$aa的引用。晕不。。。

论坛徽章:
0
10 [报告]
发表于 2010-08-06 11:31 |显示全部楼层
本帖最后由 toniz 于 2010-08-06 11:36 编辑

不一样的, ${\$aa}=1234;是想对$aa赋值。
而你的 $bb = 10;是对$b赋值。

看下面代码:
  1. $aa=123;
  2. ${\$aa}=1;
  3. print "$aa\n";

  4. $aa=123;
  5. $bb=\$aa;
  6. $bb=1;
  7. print "$aa\n";
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP