sub printElement(\%$) { my %hash1 = %{($_[0])}; my $key = $_[1]; print $key." ".$hash1{$key}."\n"; ... &printElement(\%hash, $key);
You are copying the entire hash inside the subroutine so you may as well just do it like this:
sub printElement { my ( $key, %hash1 ) = @_; print "$key $hash1{$key}\n"; ... printElement( $key, %hash );
Of course, using a reference means that you don't have to copy the hash:
sub printElement { my ( $hash1, $key ) = @_; print "$key $hash1->{$key}\n"; ... printElement( \%hash, $key );
Also, if you use the prototype (\%$) then the first argument in the call to the subroutine has to be a hash and not a reference to a hash or you will get this error message:
Type of arg 1 to main::printElement must be hash (not reference constructor) at -e line 10
In reply to Re: subroutine reference parameters
by jwkrahn
in thread subroutine reference parameters
by dnajjar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |