throop has asked for the wisdom of the Perl Monks concerning the following question:
Here on perlmonks, I've picked up the nifty way of initializing a hash
works the same as@foo{1,2,3} = qw(a b c);
which has been very handy to me several times when I've had a pair of 'parallel' lists that I wanted to turn into a hash.%foo = ( 1 => 'a' 2 => 'b' 3 => 'c')
But I don't understand why it works. I mean, @foo, %foo and $foo are three separate data-structures. @foo{@keys} = @values; looks like it's affecting @foo. But it isn't. It's affecting %foo.@foo{@keys} = @values; # instead of $foo{$_} = shift(@values) foreach @keys
Howscome?
I have an intuition that this has to do with (de)referencing of typeglobs, but I don't have nearly enough enlightenment. So I can copy the recipe by rote, but have insufficient insight into the Perl-nature of *foo.
throop
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hash initialization works, but why?
by ikegami (Patriarch) on Jun 04, 2008 at 22:44 UTC | |
|
Re: Hash initialization works, but why?
by FunkyMonk (Bishop) on Jun 04, 2008 at 22:29 UTC | |
|
Re: Hash initialization works, but why?
by thparkth (Beadle) on Jun 04, 2008 at 22:52 UTC | |
by ikegami (Patriarch) on Jun 04, 2008 at 23:09 UTC | |
by throop (Chaplain) on Jun 04, 2008 at 23:23 UTC | |
by chromatic (Archbishop) on Jun 05, 2008 at 00:56 UTC | |
by throop (Chaplain) on Jun 05, 2008 at 14:14 UTC | |
by ikegami (Patriarch) on Jun 04, 2008 at 23:31 UTC | |
by ikegami (Patriarch) on Jun 05, 2008 at 02:38 UTC | |
|
Re: Hash initialization works, but why?
by ysth (Canon) on Jun 05, 2008 at 02:48 UTC |