免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
1 [报告]
发表于 2010-08-04 16:16 |显示全部楼层
可以这样:
shift( @{ [ &func ] } );

论坛徽章:
0
2 [报告]
发表于 2010-08-05 14:40 |显示全部楼层
ft~
有这么麻烦嘛,与什么上下文无关,就一点: list是 value,array 是 variable!
由于 shift 要修改传递的参数,故只能对  variable 进行操作, 而 list 是一个 value,相当于一个常量,当然无法传递给 shift 了……

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



    黑色阳光_cu 的 warn \func(); ,说明了 return 确实是返回 list;但结合你给的代码,反而很难理解了:为什么 return 回来的 list,这里却可以修改?

论坛徽章:
0
4 [报告]
发表于 2010-08-06 09:40 |显示全部楼层
我觉得,一个正常的函数在做return @array;的时候就是返回了一个数组。
不能说func2返回的不是数组,但 ...
py 发表于 2010-08-06 09:19



    perldoc -q "a list and an array"

What is the difference between a list and an array?
  An array has a changeable length. A list does not. An array is something
  you can push or pop, while a list is a set of values. Some people make
  the distinction that a list is a value while an array is a variable.
  Subroutines are passed and return lists, you put things into list
  context, you initialize arrays with lists, and you "foreach()" across a
  list. "@" variables are arrays, anonymous arrays are arrays, arrays in
  scalar context behave like the number of elements in them, subroutines
  access their arguments through the array @_, and "push"/"pop"/"shift"
  only work on arrays.

  As a side note, there's no such thing as a list in scalar context. When
  you say

          $scalar = (2, 5, 7, 9);

  you're using the comma operator in scalar context, so it uses the scalar
  comma operator. There never was a list there at all! This causes the
  last value to be returned: 9.

我是看过这段话,所以一直记得,return 的是 list ……
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP