Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Maximum length of hash key ?

by AlfaProject (Beadle)
on Aug 19, 2012 at 16:37 UTC ( #988338=note: print w/ replies, xml ) Need Help??


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


Comment on Re^2: Maximum length of hash key ?
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" ] };
Re^3: Maximum length of hash key ?
by Marshall (Prior) 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).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://988338]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (16)
As of 2014-10-02 17:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (67 votes), past polls