Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Re: Merging hashes

by hotshot (Prior)
on Sep 10, 2002 at 12:19 UTC ( #196641=note: print w/replies, xml ) Need Help??

in reply to Re: Merging hashes
in thread Merging hashes

Got it, in fact I use references (it's my question that wasn't clear enough), the thing is that I always stay with one value for the same keys, if I'll take my example from the question, then I get only one brown entry. The references always clober each other and not added, I didn't seem to solve that yet.


Replies are listed 'Best First'.
Re: Re: Re: Merging hashes
by shotgunefx (Parson) on Sep 10, 2002 at 12:31 UTC
    Thats what hashes do. map a unique key to a unique values. But you can make that value an array ref and store your multiple values.

    It also looks like you want to apply it recursively. May I ask what you are trying to do?


    "To be civilized is to deny one's nature."
    Here's enough rope to hang yourself with.
    Merges N hashes arbitrarily deep. Making it merge arrays as well is left as an exercise for the reader.
    Dies on circular references. Not heavily tested.
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; BEGIN { my %SeenMerged = (); sub mergehashes{ # Dies on circular references my @hashrefs = @_; die "Passed a non hashref" if grep { ref $_ ne 'HASH' } @h +ashrefs; my %merged = (); my @seen = grep { ref $_ eq 'HASH' } @SeenMerged{@hashref +s}; # Break circular links.. if (@seen){ die "contains a circular reference! bailing..."; } @SeenMerged{@hashrefs} = @hashrefs; foreach my $h (@hashrefs){ while (my ($k,$v) = each %$h ){ push @{$merged{$k}}, $v; } } while (my ($k,$v) = each %merged){ my @hashes = grep { ref $_ eq 'HASH' } @$v; $merged{$k} = $v->[0] if (@$v == 1 && !ref $v->[0]); + $merged{$k} = mergehashes(@hashes) if @hashes; } delete @SeenMerged{@hashrefs}; return \%merged; } } my %hash1 = ( red => 1, brown => { green => 1, blue => { yellow => 1, }, black => 1, }, gray => 1, ); my %hash2 = ( white => 1, brown => { purple => 1, }, ); my $merged = mergehashes(\%hash1, \%hash2) ;#or die "Couldn't merge!"; print Dumper($merged);
      but if I don't work with references, and do something like:
      $hash{red}{blue} = 1; $hash{red}{green} = 1;
      I get;
      %hash = ( red => ( blue => 1, green => 1, ), );
      I want that to happen with references too.

        What I meant by "what are you trying to do?" I meant what is the problem you are trying to solve.

        Merging hashes recursively is possible but I think you might be better suited with a different data structure. Maybe I'm wrong but I can't see why you would need to do that.


        "To be civilized is to deny one's nature."

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2023-09-25 05:51 GMT
Find Nodes?
    Voting Booth?

    No recent polls found