免费注册 查看新帖 |

Chinaunix

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

问一个很菜的问题,双引号中$与@后加数组名时有什么区别 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-06-03 19:40 |只看该作者 |倒序浏览
我知道@代表数组,$代表变量的意思,代码如下:
@array = (1, 2, 3);
   $count = 1;
    while ($count <= @array) {
    print ("element $count: $array[$count-1]\n");
    $count++;
    }
print语句中的$array如果换成@array输出的结果没有区别,但代表的意思有什么不同么?

[ 本帖最后由 wine 于 2009-6-3 19:41 编辑 ]

论坛徽章:
1
未羊
日期:2014-09-08 22:47:27
2 [报告]
发表于 2009-06-03 20:33 |只看该作者
$array[$count]是访问单个数组元素,@array[$count]是数组切片。

@array[1,2,3]是由数组@array的第2、3、4个元素组成的列表,你仔细体会一下。

论坛徽章:
0
3 [报告]
发表于 2009-06-03 22:23 |只看该作者
Slicing Array Elements

You can also extract a "slice" from an array - that is, you can select more than one item from an array in order to produce another array.

@weekdays = @shortdays[0,1,2,3,4];

The specification for a slice must a list of valid indices, either positive or negative, each separated by a comma. For speed, you can also use the .. range operator:

@weekdays = @shortdays[0..4];

Ranges also work in lists:

@weekdays = @shortdays[0..2,6,7];

论坛徽章:
0
4 [报告]
发表于 2009-06-04 00:05 |只看该作者
原帖由 wine 于 2009-6-3 19:40 发表
我知道@代表数组,$代表变量的意思,代码如下:
@array = (1, 2, 3);
   $count = 1;
    while ($count  


输出没区别是你没用warning。
$array[]表示某个元素
@array表示整个数组
要输出单个元素建议用$array[]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP