如果在标量上下文中评估哈希,会得到什么? [英] What do you get if you evaluate a hash in scalar context?

查看:91
本文介绍了如果在标量上下文中评估哈希,会得到什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码段:

use strict;
use warnings;

my %a = ( a => 1,
          b => 2,
          c => 'cucu',
          d => undef,
          r => 1,
          br => 2,
          cr => 'cucu',
          dr => '321312321',

        );

my $c = %a;

print $c;

其结果是5/8,我不明白这代表什么.我在某处读到,从 fraction 查找结果中得到的数字可能代表哈希中的存储桶数,但显然并非如此.

The result of this is 5/8 and I don't understand what this represents. I read somewhere that a number from this fraction looking result might represent the number of buckets from the hash, but clearly this is not the case.

有人知道如何在标量上下文中评估perl哈希吗?

Does anyone knows how a perl hash is evaluated in scalar context?

修改

我添加了其他一些哈希值进行打印:

I added a few other hashes to print:

use strict;
use warnings;

use 5.010;


my %a = ( a => 1,
          b => 2,
          c => 'cucu',
          d => undef,
          r => 1,
          br => 2,
          cr => 'cucu',
          dr => '321312321',

        );

my $c = %a;

say $c; # 5/8


%a = ( a => 1,
       b => 21,
       c => 'cucu',
       br => 2,
       cr => 'cucu',
       dr => '321312321',

      );

 $c = %a;

say $c; # 4/8

%a = ( a => 1,
       b => 2,
       c => 'cucu',
       d => undef,
       r => 1,
       br => 2,
       cr => 'cucu',
       dr => '321312321',
       drr => '32131232122',
      );

 $c = %a;

say $c; #6/8

那么,您在哈希表中将类似于a => 1的元组"称为存储桶吗?在那种情况下,为什么最后一个散列具有9个元组"时仍将8作为分母?

So, you call a 'tuple' like a => 1 a bucket in the hash? in that case, why is the last hash still having 8 as a denominator when it has 9 'tuples' ?

谢谢大家的回应,直到现在:)

Thank you all for your responses until now :)

推荐答案

[ OP正在询问

[The OP is asking about the format of the strings returned by Hash::Util's bucket_ratio() and by a hash in scalar context before Perl 5.26. Since Perl 5.26, a hash in scalar context no longer returns a string in this format, returning the number of elements in the hash instead.]

散列是链接列表的数组.散列函数将键转换为数字,该数字用作存储该值的数组元素(存储桶")的索引.链表处理多个哈希散列到同一索引(冲突")的情况.

A hash is an array of linked lists. A hashing function converts the key into a number which is used as the index of the array element ("bucket") into which to store the value. The linked list handles the case where more than one key hashes to the same index ("collision").

分数的分母是桶的总数.

The denominator of the fraction is the total number of buckets.

分数的分子是具有一个或多个元素的存储桶数.

The numerator of the fraction is the number of buckets which has one or more elements.

对于具有相同数量元素的哈希,数字越大越好.返回6/8的碰撞比返回4/8的碰撞少.

For hashes with the same number of elements, the higher the number, the better. The one that returns 6/8 has fewer collisions than the one that returns 4/8.

这篇关于如果在标量上下文中评估哈希,会得到什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆