http://www.perlmonks.org?node_id=988338


in reply to Re: Maximum length of hash key ?
in thread Maximum length of hash key ?

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

Replies are listed 'Best First'.
Re^3: Maximum length of hash key ?
by Marshall (Canon) on Aug 20, 2012 at 08:11 UTC
    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^3: Maximum length of hash key ?
by Anonymous Monk on Aug 19, 2012 at 21:51 UTC
    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" ] };