Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I ran across this sample while reading an article by Chromatic:
my @fields = qw( name email ); my %results; @results{@fields} = ();
I mean I see what happens here:
use strict; use warnings; use Data::Dumper; my @fields = qw( name email ); my %results; print "Before: ", Dumper(\%results); @results{@fields} = (); print "After: ", Dumper(\%results); __END__ Before: $VAR1 = {}; After: $VAR1 = { 'email' => undef, 'name' => undef };
I'm just not sure why it works. How does @results resolve to %results? Apparently the use of {} must force some context since normally @results and %results refer to different variables. Thanks.

Replies are listed 'Best First'.
Re: Hash usage question
by Enlil (Parson) on May 23, 2003 at 23:24 UTC
    perldata has a good intro to slices. Which right near the end of the slice section your question is answered. :)

    -enlil

Re: Hash usage question
by chromatic (Archbishop) on May 24, 2003 at 00:34 UTC

    You're right; it is indeed the subscripting punctuation that clarifies whether it's an array or a hash. In Perl 5, the leading sigil signifies what you want out of the data structure -- a scalar, a list, or a hash. In Perl 6, the leading sigil will remain constant, and context will determine what you get out of it.