免费注册 查看新帖 |

Chinaunix

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

perl中访问散列的键对应的值时候键到底需不需要引号括起来? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-02-11 01:00 |只看该作者 |倒序浏览
[root@node254 ~]# perl
%student = ( "Math" => {"joe" => 95,"peter"=> 100},
             "Science" => {"joe"=> 100,"peter" =>80 }
             );
print "on the Math test joe got ";
print "$student{Math}->{joe}.\n";
__END__
on the Math test joe got 95.


[root@node254 ~]# perl
%student = ( "Math" => {"joe" => 95,"peter"=> 100},
             "Science" => {"joe"=> 100,"peter" =>80 }
             );
print "on the Math test joe got ";
print "$student{'Math'}->{'joe'}.\n";
__END__
on the Math test joe got 95.

[root@node254 ~]# perl
%student = ( "Math" => {"joe" => 95,"peter"=> 100},
             "Science" => {"joe"=> 100,"peter" =>80 }
             );
print "on the Math test joe got ";
print "$student{\"Math\"}->{\"joe\"}.\n";
__END__
on the Math test joe got 95.


在上面的例子中键有没有引号好象都可以呀,请大侠解惑

[root@node254 ~]# perl -v

This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi

论坛徽章:
0
2 [报告]
发表于 2011-02-11 01:14 |只看该作者
回复 1# aoma


    看到老外有说到这句话: It is not necessary to quote the key if it is a single word
是不是说如里{}里的键是一个词(我觉得是一个字符串的话)就不用引号括起来的意思

论坛徽章:
0
3 [报告]
发表于 2011-02-11 08:36 |只看该作者
我觉得是的 因为key本身是字符串,即使用数字,也会当成字符串的

论坛徽章:
0
4 [报告]
发表于 2011-02-11 08:46 |只看该作者
当你键值不是1个词的时候,不引起来会导致语法错误
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP