Sounds like you've already got the idea, but this is an issue that trips a lot of us up at one time or another. I might as well take a stab at it myself.

It's hard to look at the confusing bit in isolation. I know, because I just spent 15 minutes trying to describe it without any additional context. Maybe if I did the backwards reading trick I could describe what's going on.

Here's the original baffling line.

@{ $n{ $mag } }{ @keys } = ( 0 ) x @keys;

Here's the line broken down by reading backwards.

( 0 ) x @keys # A list of zeroes the same length as @keys = # is assigned to { @keys } # a slice using @keys for the keys @{ # in the hash referred to $n{$mag} # by the value pointed to by $mag in %n. }

That makes a little more sense, I guess. I don't care for this idiom, personally, but I like my code to be explicit whenever possible - even if it means writing out my intent in more than one statement :-) Oddly enough, though, the most explicit version I could come up with was actually shorter!

$n{$mag} = { map { $_ => 0 } @keys };

Well, I suppose I could have used a foreach loop instead, and that would have been nice and explicit also. I must finally be starting to get the hang of map or something.


In reply to Re: Is this a hash slice? by webfiend
in thread Is this a hash slice? by tphyahoo

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.