in reply to Multiple Hashes are stressing me

How about:
%bighash = (%hash1,%hash2,%hash3);
Note that any duplicate keys will collide, and the value will be that of the the right most hash value... i.e. $hash3{dupe} will take precedence over $hash2{dupe} will take precedence over $hash1{dupe}.

Here is an example:

#!/usr/bin/perl -wT use strict; my %hash1 = (2 => 'cars', 1 => 'motorcycle', 4 => 'balloons', 9 => 'came from hash1'); my %hash2 = (5 => 'airplanes', 3 => 'helicopters', 6 => 'skateboards', 9 => 'came from hash2'); my %bighash = (%hash1 , %hash2); # combine the two hashes print "Hash1\n"; print "\t$_ => $hash1{$_}\n" for (sort {$a<=>$b} keys %hash1); print "Hash2\n"; print "\t$_ => $hash2{$_}\n" for (sort {$a<=>$b} keys %hash2); print "BigHash\n"; print "\t$_ => $bighash{$_}\n" for (sort {$a<=>$b} keys %bighash); =OUTPUT Hash1 1 => motorcycle 2 => cars 4 => balloons 9 => came from hash1 Hash2 3 => helicopters 5 => airplanes 6 => skateboards 9 => came from hash2 BigHash 1 => motorcycle 2 => cars 3 => helicopters 4 => balloons 5 => airplanes 6 => skateboards 9 => came from hash2

-Blake

Replies are listed 'Best First'.
Re: Re: Multiple Hashes are stressing me
by DaWolf (Curate) on Oct 09, 2001 at 04:17 UTC
    Is it that simple?

    Man, I'm really embarassed...

    Thanks a lot, Blake. I'll test it tomorrow at work.

    Best Regards,

    Er Galvão Abbott
    a.k.a. Lobo, DaWolf
    Webdeveloper
      Whats actually happening is that the hashes are flattened into a list, then this list is assigned to a new hash. So, %h1 and %h4 wind up with the same contents below:
      #!/usr/bin/perl -wT use strict; my @a1 = ('jan','January','feb','February'); my @a2 = ('mar','March', 'apr','April'); my %h1 = (@a1, @a2); # Two arrays become one h +ash my %h2 = ('jan','January','feb','February'); my %h3 = ('mar'=>'March' ,'apr'=>'April'); # '=>' is really just a c +omma my %h4 = (%h2,%h3); # Hashes get flattened an +d behave # nearly the same as the +two # arrays did above print "h1\n"; print "\t$_ => $h1{$_}\n" for (sort keys %h1); print "h4\n"; print "\t$_ => $h4{$_}\n" for (sort keys %h4); =OUTPUT h1 apr => April feb => February jan => January mar => March h4 apr => April feb => February jan => January mar => March

      -Blake

      Yes, it gets easier once you realize that you don't have to micromanage your data, like in C.