in reply to Combining hashes

Well, this way would work... but there are others. Such as:
my $total = scalar(keys %class1) + scalar(keys %class2) + scalar(keys +%class3); #or my $totb = 0; $totb += $_ for map { scalar keys %$_; } (\%class1, \%class2, \%class3 +);
As for merging, I believe the answer to be no. A hash can not have two keys of the exact same value. You can create a union of them easily enough by using
my %new = (%class1, %class2, %class3);
You will however lose any duplicates.

Replies are listed 'Best First'.
Re: Re: Combining hashes
by MeowChow (Vicar) on May 11, 2001 at 22:08 UTC
    I believe that mpolo actually wants to lose the duplicates, since his intention is to count the total number of countries representated between employee classes.

    Also, you can rewrite:

    my $totb = 0; $totb += $_ for map { scalar keys %$_; } (\%class1, \%class2, \%class3 +);
    as the following:
    my $totb = 0; $totb += keys %$_ for \(%class1, %class2, %class3);
    No need to iterate twice.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
      Very good point. I felt that something like that could be done to avoid the second iteration. I just gave up before I found it, thanks for showing me.