good chemistry is complicated,and a little bit messy -LW PerlMonks

### Maximum length of hash key ?

 on Aug 19, 2012 at 15:49 UTC ( #988335=perlquestion: print w/ replies, xml ) Need Help??
AlfaProject has asked for the wisdom of the Perl Monks concerning the following question:

Hello , What is the maximum length of hash key ? Thanks

Comment on Maximum length of hash key ?
Re: Maximum length of hash key ?
by NetWallah (Monsignor) on Aug 19, 2012 at 16:23 UTC
There is no practical limit other than it has to fit in memory.

What is your use case, and what problem are you trying to solve ?

I hope life isn't a big joke, because I don't get it.
-SNL

My problem was to merge same hash values when their keys must be combined too.
For ex. %h=( a=>123, b=>123, c=>12);
the output will be :
%h =( "a;b"=>123 ,c=>12);
But because I wasn't sure about the allowed size of the key , I have used md5 hashes for keys. After that I used a temp hash of arrays to store different keys .
But nice to know that it's not limited , makes life much easier ;)
For ex. %h=( a=>123, b=>123, c=>12); the output will be : %h =( "a;b"=>123 ,c=>12);
... sounds like an inverted list, to me.
```%h = { "123" => [ "a", "b" ], "12" => [ "c" ] };
Is this going in the right direction for you?
Sounds like you just need a HoA (Hash of Array).
```#!/usr/bin/perl -w
use strict;
use Data::Dump qw(pp);

my %h=( a=>123, b=>123, c=>12);

print pp \%h; #{ a => 123, b => 123, c => 12 }
print "\n";

# Access each key's value in the %h hash...
#
# Each "new key" in the reversed hash is a
# unique value from the %h hash, i.e., \$hash{\$key}
#
# But each one of these new "keys" can contain
# multiple values, so that means that the
# reversed hash has to be a more complex data
# structure, a hash of pointers to array...
#
# Each one of the keys of the "reversed hash" is
# a value from the original hash. They now become keys
# of that "reversed hash" and they have as a value,
# a reference to an array of the keys of the original hash.

my %reversed;
foreach my \$key (keys %h)
{
# the value of \$h{\$key} is the new key
# could have been:
#    my \$new_key = \$h{\$key};
#    push @{\$reversed{\$new_key}}, \$key;
# but this is the same...

push @{\$reversed{\$h{\$key}}}, \$key;
}

print pp \%reversed; #{ 12 => ["c"], 123 => ["a", "b"] }
print "\n";

__END__
{ a => 123, b => 123, c => 12 }
{ 12 => ["c"], 123 => ["a", "b"] }
Update: We are very far from needing MD5 keys. I am responding to your clarification of what you want for the output. Please respond to my post with other details if I didn't get it right...And I don't see that this has to do with maximum key length (which is pretty much unlimited).
Re: Maximum length of hash key ?
by BrowserUk (Pope) on Aug 19, 2012 at 16:47 UTC

if your application calls for seriously long(*) hash keys, I suggest that you consider combining the MD5 of the real key, with its length, and use that as a substitute for the actual key.

This combination is pretty much (but not quite) guaranteed to be as unique as the long keys, but it will require less memory.

(*say anything much over 25 bytes).

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Re: Maximum length of hash key ?
by swampyankee (Parson) on Aug 19, 2012 at 22:51 UTC

I made a little test program. The maximimum key length is at least 25,000,000 characters (I killed the program when key length = 2^28).

Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

Re: Maximum length of hash key ?
by sundialsvc4 (Monsignor) on Aug 20, 2012 at 14:22 UTC

It’s a principle in databases, and it applies here also, that a “key” should be nothing more or less than a record locator.   It ought not convey anything about the record being located; it should not be part of the data.   In this case, though, a hash value such as abc;def clearly is “a list of important values.”   Therefore, IMHO, it has absolutely no business being a hash-key.

If you find yourself wondering how long a hash-key value can be ... you are doing something very, very wrong.   :-)

This might be an excellent application for an SQLite database file.   Instead of busting the top off of your memory allotment, put the data into a special kind of file that is built for searching using SQL strings.   As long as you remember to use transactions so as to get lazy disk-writes, it is rugged and fast.

Re: Maximum length of hash key ?
by AlfaProject (Beadle) on Aug 20, 2012 at 15:38 UTC
The problem is that value can be a few thousands of chars, so I wasn't sure if it's ok to put it into a hash-key.
I used this approach :
```use Digest::MD5 qw(md5 md5_hex md5_base64);
# do unique value
my %write_short;
for my \$index(keys %write){
#print "index \$index\n";
my %trg = %{\$write{"\$index"}};
my %md5_line;
my %md5_tg;
for my \$target( keys %trg ){
my \$digest = md5_base64(\$trg{"\$target"});

\$md5_line{"\$digest"}=\$trg{"\$target"};
push @{\$md5_tg{"\$digest"}},\$target;
}

for my \$digest(keys %md5_tg){
my \$tg_line=join ',', @{\$md5_tg{"\$digest"}};
\$write_short{"\$index"}{"\$tg_line"}=\$md5_line{\$digest};
}
}

Create A New User
Node Status?
node history
Node Type: perlquestion [id://988335]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2013-05-19 05:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?