S
Shashank Khanvilkar
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";
}
}
}
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";
}
}
}