Passing hashes to a function

From: Shashank Khanvilkar (shashank_at_mia.ece.uic.edu)
Date: 12/10/04


Date: Fri, 10 Dec 2004 13:47:16 -0600

Hi,

I will appreciate if someone can give me any pointers on how to do this?

What I need:
-------------
I have two hashes (%h1 and %h2) defined as below:

$h1{a}{b} = 10;
$h1{a}{c} = 1;
$h1{b}{c} = 10;

$h2{f} = 1;
$h2{g} = 2;

I want to pass these two hashes to a function only by VALUE (i.e the
function should make a copy of the hash and may change the hash values
in this copy. But when the function returns the changes should not
affect either %h1 or %h2)

What Have I tried:
------------------
First I tried passing the hash by value. But then I realized that hashes
get flattend out. Hence this was not useful.
Then I tried passing the hashes by reference as given in the below program.
However, I observe some wierd stuff. In the below program I have two
functions passHashRef_1 and passHashRef_2.
passHashRef_1 modified Hoh(copy of %h1), while passHashRef_2 modifies
copy of %h2.
The original %h2 remains unchanged. But the original %h1 changes.. Why
is that?

#!/usr/bin/perl

my %h1;

$h1{a}{b} = 10;
$h1{a}{c} = 1;
$h1{b}{c} = 10;

$h2{f} = 1;
$h2{g} = 2;
print "Before: "; print_HoH(%h1);
passHashRef_1(\%h1, \%h2);
print "After: "; print_HoH(%h1);

print "Before: "; print_hash(%h2);
passHashRef_2(\%h1, \%h2);
print "After: "; print_hash(%h2);

sub passHashRef_1 {
     my ($a, $b) = @_;
     my %aa = %{$a};
     my %bb = %{$b};

     $aa{"a"}{"b"} = 100;
     print "passHashRef_1: "; print_HoH(%aa);

}

sub passHashRef_2 {
     my ($a, $b) = @_;
     my %aa = %{$a};
     my %bb = %{$b};

     $bb{"f"} = 100;
     print "passHashRef_2: "; print_hash(%bb);

}

sub print_hash {
     my (%Hsh) = @_;
     foreach $x (keys %Hsh) {
         print "$x==>$Hsh{$x}, ";
     }
     print "\n";
}

sub print_HoH {
     my (%graph) = @_;

     foreach $x (sort keys %graph) {
         foreach $n2 (sort keys %{$graph{$x}}) {
             print "($x, $n2) = $graph{$x}{$n2}\n";
         }
     }

}



Relevant Pages

  • Re: People ~Fing with Life
    ... That is what the charge was. ... hash values and the like'. ... this data area had no corresponding entry in the allocation tables. ... Hashes are used for the purposes of error correction ...
    (uk.legal)
  • RE: [7.8.2002 44916] Notice of Copyright Infringement]
    ... Appending a single bit onto the end of the file makes a different hash. ... and you no longer match the hashes. ... The only way to prove you're breaking copyright is to download at ... |"real" warezed version of whatever movie. ...
    (Vuln-Dev)
  • Re: ACCEPT and the SCREEN SECTION.
    ... General-purpose hashes, for hash tables and similar data structures, ... In short, cryptographic hashes like MD5 are used to verify data, ... if a client asks for and receives a chunk ...
    (comp.lang.cobol)
  • Re: what is probability to create two equal hashes for md5 algorithm
    ... Other than that, if the hash is statistically good, the longer the hash, the ... few cases the hashes match. ... md5 and crc32)? ... How much does it cost to compare two hashes? ...
    (sci.crypt)
  • Re: Parsing problem
    ... > I get into hashes I get a headache. ... First off a 'hash' is simply an 'associative array'. ... They are in turn a reference to a hash. ... a punchcard column formated cobol wingDingDingDing style ...
    (perl.beginners)