Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do you get if you evaluate a hash in scalar context?

Tags:

Consider the following snippet:

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; 

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.

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

Edit

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 

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 :)

like image 766
Tudor Constantin Avatar asked Sep 15 '11 07:09

Tudor Constantin


People also ask

What do you get if you evaluate a hash in list context?

The assignment of a hash to a list type in Perl is accomplished by making a list with the keys and values as its elements. When a hash is in LIST context, Perl converts a hash into a list of alternating values.

What do you get if you evaluate a hash in list context in Perl?

When a hash is in LIST context Perl converts a hash into a list of alternating values. Each key-value pair in the original hash will become two values in the newly created list. For every pair the key will come first and the value will come after.

What does scalar function do in Perl?

scalar keyword in Perl is used to convert the expression to scalar context. This is a forceful evaluation of expression to scalar context even if it works well in list context.

What are the ways in which hash values can be manipulated?

There are two ways to initialize a hash variable. One is using => which is called the fat arrow or fat comma. The second one is to put the key/value pairs in double quotes(“”) separated by a comma(,). Using fat commas provide an alternative as you can leave double quotes around the key.


1 Answers

[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.

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.

like image 141
ikegami Avatar answered Oct 01 '22 20:10

ikegami