免费注册 查看新帖 |

Chinaunix

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

请教一个pseudohash相关的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-10-05 16:20 |只看该作者 |倒序浏览
在看《Programming Perl》3rd Edition,请教一个问题。

8.3.5. Pseudohashes

A pseudohash is any reference to an array whose first element is a reference to a hash. You can treat the pseudohash reference as either an array reference (as you would expect) or a hash reference (as you might not expect). Here's an example of a pseudohash:

$john = [ {age => 1, eyes => 2, weight => 3}, 47, "brown", 186 ];

The underlying hash in $john->[0] defines the names ("age", "eyes", "weight") of the array elements that follow (47, "brown", 186). Now you can access an element with both hash and array notations:
$john->{weight}             # Treats $john as a hashref
$john->[3]                  # Treats $john as an arrayref

我试了一下,执行 "print $john->[3];" 能得到"186",可 "print $john->{weight};" 却提示 "Not a HASH reference",没能按其所说的得到"186",是为什么?

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
2 [报告]
发表于 2010-10-05 16:40 |只看该作者
书上说是试验功能,现在看样子是取消了吧?这玩意太 bug 了

论坛徽章:
0
3 [报告]
发表于 2010-10-05 19:41 |只看该作者
书上说是试验功能,现在看样子是取消了吧?这玩意太 bug 了
zhlong8 发表于 2010-10-05 16:40


嗯,谢谢,确实是。

http://perldoc.perl.org/perlref. ... -an-array-as-a-hash

Pseudo-hashes have been removed from Perl.

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
4 [报告]
发表于 2010-10-06 14:01 |只看该作者
回复 3# nullplace


    还是你厉害,我查 perlref 没找到,还以为这个怪物文档根本没讲呢,忘记 ctrl+f 了

论坛徽章:
0
5 [报告]
发表于 2010-10-06 16:41 |只看该作者
$john->[0]->{weight}就ok了,因为$john->[0]保存才是匿名散列表的指针,当然需要使用箭头操作符了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP