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

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

问题描述

考虑以下片段:

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 正在询问 Perl 5.26 之前的标量上下文中散列返回的字符串的格式.从 Perl 5.26 开始,标量上下文中的散列不再返回这种格式的字符串,而是返回散列中元素的数量.如果你需要这里讨论的值,你可以使用 Hash::Utilbucket_ratio().]

[The OP is asking about the format of the string returned 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. If you need the value discussed here, you can use Hash::Util's bucket_ratio().]

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

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天全站免登陆