Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is 4/16 in hashes?

Tags:

perl

if (%hash){
     print "That was a true value!\n";
}

That will be true if (and only if) the hash has at least one key-value pair.

The actual result is an internal debugging string useful to the people who maintain Perl. It looks something like "4/16," but the value is guaranteed to be true when the hash is nonempty, and false when it's empty. --Llama book

What is this 4/16? Can anyone show me a small program from where I can see that the result is 4/16?

like image 587
Chankey Pathak Avatar asked Jul 17 '11 10:07

Chankey Pathak


People also ask

What is a 16 bit hash?

16bits is enough to be stored in an integer and could simply be incremented if you just need a way to identify an object (limited to 65536). If nevertheless you want to use an hash function, you could try to use something very simple such as Pearson hashing that will produce a 8bit hash.

How are hashes calculated?

Hashing is simply passing some data through a formula that produces a result, called a hash. That hash is usually a string of characters and the hashes generated by a formula are always the same length, regardless of how much data you feed into it. For example, the MD5 formula always produces 32 character-long hashes.

What is 32bit hash?

The 32-bit long hash value is a hexadecimal number of 8 characters. MD4 is a Message Digest Algorithm developed by Ronald L. Rivest from RSA Data Security, Inc. Currently it's considered insecure, but it's very fast on 32-bit mashines and it's used for calculating EDonkey 2000 hashes in the EDonkey p2p network.

What number base is the hash in?

This is because the hash is using base 16 (hexadecimal) numbering.


1 Answers

From perldoc perldata:

If you evaluate a hash in scalar context, it returns false if the hash is empty. If there are any key/value pairs, it returns true; more precisely, the value returned is a string consisting of the number of used buckets and the number of allocated buckets, separated by a slash. This is pretty much useful only to find out whether Perl's internal hashing algorithm is performing poorly on your data set. For example, you stick 10,000 things in a hash, but evaluating %HASH in scalar context reveals "1/16" , which means only one out of sixteen buckets has been touched, and presumably contains all 10,000 of your items.

so, 4/16 would be the buckets used/allocated count, and something like the following will display this value:

%hash = (1, 2);
print scalar(%hash); #prints 1/8 here
like image 90
Hasturkun Avatar answered Sep 24 '22 07:09

Hasturkun