in reply to beyond me

this is called a hash slice.
someone has declared o as a hash, and wants to set up 3 elements in semi-odd way.
it's equivalent to
%o= (a=>'a', b=>'b', c=>'c');
except that you can't declare a hash under strict this way -- you still need to explicity code my %o, and then use the hash slice.

So what's it good for?
Primarily when you're populating an array from a hash, and you only want to use a subset of the hash's values. It can be onerous to type @ary=($hash{e}, $hash{f}, ... $hash{n}), if you only wanted those particular elements, so the almight Wall-Oz gave us the hash slice, which reduces the assignment to @ary=@hash{"e".."n"} (yes, there are other ways of doing this, I acknowledge)
Here's a small example for you showing the hash slice in action :

use strict; my %o; @o{qw(a b c d e f)} = (1,2,3,4,5); my @foo; @foo=@o{"c".."e"}; print @foo;
the third line uses a hash slice to assign 5 values to %o. the fifth line uses a hash slice to easily take 3 elements from %0, and assign their values into @foo.
Also, even though I haven't demonstrated it, using
@o("a","e","f")
is just as legal.

See also array slices for a similar concept, but with arrays.

update : hey, neat! @foo=@bar{sort keys %bar};

Replies are listed 'Best First'.
Re: How I learned to stop worrying and love the hash slice. (boo)
by jehuni (Pilgrim) on Jul 25, 2001 at 22:39 UTC

    So, why can't you intialize a slice under 'use strict'? In fact, even under 'no strict', you can't initialize a slice with 'my' nor 'our'. Same thing with array slices.

    I ran into this restriction recently, and was just curious as to the reason.

    -jehuni

      Probably because "my @foo{qw(a b c)} = (1 .. 3)" indicates to the compiler that you're trying to create a list, but it then sees the {} and gets confuzzled.

      Update: If you don't use strict and you don't use my or our, the compiler is fine. :)

Re: How I learned to stop worrying and love the hash slice. (boo)
by PrakashK (Pilgrim) on Jul 25, 2001 at 22:59 UTC
    I frequently use hash slices to eliminate duplicates from an array.

    A simplified example:

    my @a = (1,2,3,2,1); my %a; @a{@a} = (1) x @a; my @unique_a = keys %a;
    Of course, this does not preserve the order of the elements as they were in original array @a.
      Perhaps grep might work better here?
      my @a = (1,2,3,2,1); my %seen; my @unique_a = grep {!$seen{$_}++} @a; print "A: $_\n" for (@a); print "Unique: $_\n" for (@unique_a);
      Assuming you wanted to keep the first occurance, this will preserve the original order.

      Or you can use map to create your %a hash.

      my @a = (1,2,3,2,1); my %a = map {$_=>1} @a; my @unique_a = keys %a; print "A: $_\n" for (@a); print "Unique: $_\n" for (@unique_a);

      -Blake