Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
In the code example, I pass a hash by value to a sub, push_1, and push one onto the array,
@{ $hash{$key} }But when I examine the hash after the sub call, the changes are in the hash, but why is this happening. I thought the hash should have the pre function call values.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash; @hash{ 'a'..'d' } = ([175, 450], [0, 350], [0, 50], [300, 50]); print "Initial hash\n"; print Dumper \%hash; print "\nhash after call\n"; push_1(%hash); print Dumper \%hash; sub push_1 { my %hash = @_; for (keys %hash) { push @{ $hash{$_} }, 1; } } __END__ *** Output C:\perlp>perl t.pl Initial hash $VAR1 = { 'c' => [ 0, 50 ], 'a' => [ 175, 450 ], 'b' => [ 0, 350 ], 'd' => [ 300, 50 ] }; hash after call $VAR1 = { 'c' => [ 0, 50, 1 ], 'a' => [ 175, 450, 1 ], 'b' => [ 0, 350, 1 ], 'd' => [ 300, 50, 1 ] }; C:\perlp>
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Pass by value acts like pass by reference
by tilly (Archbishop) on Jul 11, 2004 at 16:38 UTC | |
Re: Pass by value acts like pass by reference
by sauoq (Abbot) on Jul 11, 2004 at 20:15 UTC | |
Re: Pass by value acts like pass by reference
by davido (Cardinal) on Jul 11, 2004 at 16:22 UTC | |
Re: Pass by value acts like pass by reference
by Anonymous Monk on Jul 11, 2004 at 16:42 UTC | |
by demerphq (Chancellor) on Jul 11, 2004 at 19:30 UTC | |
by Anonymous Monk on Jul 12, 2004 at 20:12 UTC | |
by tilly (Archbishop) on Jul 11, 2004 at 16:43 UTC | |
Re: Pass by value acts like pass by reference
by gmpassos (Priest) on Jul 12, 2004 at 07:19 UTC |
Back to
Seekers of Perl Wisdom