免费注册 查看新帖 |

Chinaunix

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

hash 的一点问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-02-14 11:25 |只看该作者 |倒序浏览
  1. [root@localhost ~]# perl -e '%a=(a,aa,b,bb,c,cc,d,dd,e,ee);$b=%a;print $b,"\n"'
  2. 5/8
  3. [root@localhost ~]# perl -e '%a=(a,aa,b,bb,c,cc,d,dd,e,ee,f,ff);$b=%a;print $b,"\n"'
  4. 6/8
  5. [root@localhost ~]# perl -e '%a=(a,aa,b,bb,c,cc,d,dd,e,ee,f,ff,g,gg);$b=%a;print $b,"\n"'
  6. 6/8
复制代码
为啥后面2个是一样的,明显多了一个。
问了几个人都说诡异。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2011-02-14 12:15 |只看该作者
502 只住两个人,602 合租住了 8 个人,房子面积居然是一样的。诡异。后来 602 又搬走了 2 个人,只剩下 6 个人,面积居然没有减少!诡异!

论坛徽章:
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
3 [报告]
发表于 2011-02-14 12:49 |只看该作者
明显 'g' 和别人同房了

论坛徽章:
0
4 [报告]
发表于 2011-02-14 13:08 |只看该作者
回复 1# minmig


   
http://perldoc.perl.org/perldata.html#Scalar-values

If you evaluate a hash in scalar context, it returns falseif the hash is empty. If there are any key/value pairs, it returns true; moreprecisely, the value returned is a string consisting of the number of usedbuckets and the number of allocated buckets, separated by a slash. This ispretty much useful only to find out whether Perl's internal hashing algorithmis performing poorly on your data set. For example, you stick 10,000 things ina hash, but evaluating %HASH in scalar context reveals "1/16", which means only one out of sixteen buckets has beentouched, and presumably contains all 10,000 of your items. This isn't supposedto happen. If a tied hash is evaluated in scalar context, a fatal error willresult, since this bucket usage information is currently not available for tiedhashes.

You can preallocate space for a hash by assigning to thekeys() function. This rounds up the allocated buckets to the next power of two:

1.      keys(%users)= 1000;        # allocate 1024 buckets



Also refer to <<Professional Perl Programming>> Page 137
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP