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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |