in reply to beyond me
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.%o= (a=>'a', b=>'b', c=>'c');
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 :
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.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;
is just as legal.@o("a","e","f")
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 | |
by dragonchild (Archbishop) on Jul 25, 2001 at 22:52 UTC | |
|
Re: How I learned to stop worrying and love the hash slice. (boo)
by PrakashK (Pilgrim) on Jul 25, 2001 at 22:59 UTC | |
by blakem (Monsignor) on Aug 14, 2001 at 14:24 UTC |