in reply to Merging two @ARRAYS into a %HASH

I didn't get it! :o(

I always knew you could do $this, @this and %this (and even *this) within the same program...
But what's this new @this{}?

#!/home/bbq/bin/perl
# Trust no1!

Replies are listed 'Best First'.
RE: RE: Merging two @ARRAYS into a %HASH
by chromatic (Archbishop) on Apr 22, 2000 at 09:30 UTC
    It's a hash slice! Hash slices are wonderful things. Instead of accessing one key at a time, you can access a list of keys:
    my %foo = ( 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, ); print @foo{ 'one', 'two' }, "\n"; print @foo{ 'three', 'four' }, "\n";
    You can assign values to them too:
    my %foo = (); @foo{ 'one', 'two' } = (1, 2); @foo{ 'three', 'four' } = (3, 4); print keys %foo;
RE: RE: Merging two @ARRAYS into a %HASH
by btrott (Parson) on Apr 22, 2000 at 10:23 UTC
    Everything chromatic says, but also, I thought I'd add that hash slices are very useful for stuffing an array into a hash (as in the original example). For example, say that you had a list of elements and you wanted to check if certain items were in that list. The best way to do this is with a hash, generally, and it's very easy to set up such a hash for lookups:
    my @list = qw/foo bar baz/; my %hash; @hash{@list} = (); # now I can test if an element is in @list # by checking if the key exists in %hash if (exists $hash{"foo"}) { print "found foo!"; }