# Compiler directives and Includes use strict; use warnings; use diagnostics; # Global declarations; # Constants and pragmas use constant TRUE => scalar 1; use constant FALSE => scalar 0; ###########################################################; #main() { my $value1 = 0; my $value2 = 0; my $ref = RtnRef(); my $key = ''; # this line will rtn both the key and the value from the hash # foreach $key (%$ref){ foreach $key (keys %$ref){ # this works # ($value1, $value2)= @$ref{$key} # @$ref{$key} rtns array ref ($value1, $value2)= @{$ref->{$key}} # rtns array vals ##do something... } #However when I pass the same $ref to a sub routine mysub($ref); exit; } #end main() ############################################################; sub mysub{ # my $param = (@_); # this rtns scalar @_ (== 1)) # my ($param) = (@_); # this works ($param = $ref) my $param = shift; # and so does this my $key = ''; my $value1 = 0; my $value2 = 0; # foreach $key (%$param){ foreach $key (keys %$param){ # ($value1, $value2)= @$param{$key} ($value1, $value2)= @{$param->{$key}} ##do something... } return; } ###########################################################; sub RtnRef{ my %myhash = (); my $key = 'key1'; $myhash{'key1'} = [23, 40]; $myhash{'key2'} = [01, 50]; $myhash{'key3'} = [99, 22]; return \%myhash; } ############################################################;
In reply to Re: A dereferencing problem
by periapt
in thread A dereferencing problem
by Scarborough
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |