in reply to Re^2: passing multi dimensional hashes into a sub
in thread passing multi dimensional hashes into a sub

Never use %hash_ref, its not a "ref"erence, its a hash, so don't call it a reference

Also never use %hash either, the % part tells you its a hash, the "hash" part is supposed to describe whats inside this dictionary, which "hash" doesn't do

Outside of an introduction to hashes never use hash unless you're storing md5sum or some such, in which case you still shouldn't use it, unless the type of hash isn't set (can be md5 or sha1 or ... so the generic hash fits)

See age of peter, sum of bob and Subroutines: Returning a hash vs. hash reference and free book Modern Perl

So taking all this into account

my $ScoresRef = GetScores( "testscores.txt" ); sub GetScores { my( $file ) = @_; ## todo, read file for real my %Scores; $Scores{tim } = [ 40 , 70, 50, 80 ]; $Scores{john} = [ 98, 97 , 100, 89 ]; $Scores{eden} = [ 87, 56, 89, 97 ]; $Scores{pepe} = [ 93 ,91, 94, 90 ]; $Scores{leah} = [ 100, 99, 99, 100 ]; $Scores{tony} = [ 89, 94, 100, 89 ]; $Scores{matt} = [ 68, 70, 75, 73 ]; return \%Scores; }

Do you see when you would use it %Scores versus $ScoresRef?

{ my %Scores; FillScores( \%Scores , "testscores.txt"); delete $Scores{john}; } sub FillScores { my( $ScoresRef , $file ) = @_; ## todo, read file for real $ScoresRef->{tim } = [ 40 , 70, 50, 80 ]; $ScoresRef->{john} = [ 98, 97 , 100, 89 ]; $ScoresRef->{eden} = [ 87, 56, 89, 97 ]; $ScoresRef->{pepe} = [ 93 ,91, 94, 90 ]; $ScoresRef->{leah} = [ 100, 99, 99, 100 ]; $ScoresRef->{tony} = [ 89, 94, 100, 89 ]; $ScoresRef->{matt} = [ 68, 70, 75, 73 ]; return $ScoresRef; }

So, we use hash references to save memory,

and we use hashes to avoid typing  ->

once you get used to the idea of typing  -> , you use hashes to avoid scaring noobs :)