One thing that hasn't been mentioned yet in this thread is that in your example, you are only looking at one value at a time: either the hash key 'a' or the array index 0. If this is true in your real code as well, then you don't need to be using slices. In fact I would say you should not use slices for a couple of reasons:

To see what I mean by that last one, try this code:

my $hashref = {}; # initializer not needed, but helps clarity $hashref->{a} = localtime; # can also be written $$hashref{a} print "$hashref->{a}\n"; @$hashref{a} = localtime; # this is a slice because of the @ print "$hashref->{a}\n";

The second assignment evaluates localtime in list context, but because the slice on the left side only has one element, only the first value in the returned list gets stored there, which happens to be the number of seconds.

My preference is to be as explicit as possible so I write $hashref->{a} for single elements and @{$hashref}{'a','b'} for slices.


In reply to Re: A better understanding of array and hash references by Errto
in thread A better understanding of array and hash references by jeanluca

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.