Here is one shot, though I changed some of the constants:

#!perl -w

my %h1  = ("one" => 1, "two" => 2, "three" => 3);
my %h2  = ("four" => 4, "five" => 5, "six" => 6);
my %h3  = ();

mergehash( \%h1, \%h2, \%h3);


sub mergehash {
    my ( $h1, $h2, $h3) = @_;
    foreach my $MyKey1 ( keys %{$h1} ) {
        $h3->{$MyKey1} = $h1->{$MyKey1};
     }

    foreach my $MyKey2 ( keys %{$h2} ) {
        $h3->{$MyKey2} = $h2->{$MyKey2};
     }

    foreach my $MyKey (sort keys %{$h3}) {
        printf "%-9s: %3d\n", $MyKey, $h3->{$MyKey};
     }
    
 } # end of mergehash

Output:
five     :   5
four     :   4
one      :   1
six      :   6
three    :   3
two      :   2

Wags ;)

-----Original Message-----
From: AMORE,JUAN (HP-Roseville,ex1) [mailto:[EMAIL PROTECTED]]
Sent: Saturday, November 03, 2001 10:09
To: Beginners@Perl. Org (E-mail)
Cc: 'Maxim Berlin'
Subject: merging two hashes together?


Hello Perl Gurus,
I'm trying to write a program that will merge two hashes together onto a
third hash called %h3. I need to
return a reference to %h3 to the main program and print each of the values
of the hash %h3 using the arrow notation. 
Name of the actual function performing the merge mergehash.  Pass %h1 and
%h2 to mergehash as references.
The function call in the main program will be something like the following
where $ref_h3 will be used to print the values in %h3. 
        
                $ref_h3 = &mergehash(_____, _____);

%h1     = ("one" => 1, "two" => 2, "three" => 3);
%h2     = ("four" => 4, "five" => 5, "six" => 6);



Many Thanks!!!:)
JA





-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to