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 :)


In reply to Re^3: passing multi dimensional hashes into a sub (when to use a hash or vs hashref ) by Anonymous Monk
in thread passing multi dimensional hashes into a sub by perlynewby

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.