* [EMAIL PROTECTED] ([EMAIL PROTECTED]) wrote:
> >>
> > #  foreach $number (qw <en tre ni>) {
> >   foreach $number ($extract) {
> >      push (@return_array, $hash_table{$number});
> >   }
> >   return @return_array;
> 
> return @hash_table{ @extract };
> 
> >>>>This will return a list of values from %hash_table using the keys from
> >>>>@extract.
> 
> Please explain why this works.  thank you.

It's called a hash slice because it gives you a 'slice' of the hash
you're working with.  First I'll define a hash for the example:

    my %hash = (
        name       => 'Jeff Bisbee',
        occupation => 'Perl Programmer',
        email      => '[EMAIL PROTECTED]',
    );

You access a slice of that hash by providing an array of keys and the
hash slice will return an array of values.

    my @key_array = qw(name email);
    my ($name, $email) = @hash{ @key_array };

Or just write it all in one line...

    my ($name, $email) = @hash{ qw(name email) };

Hash slices provide a powerful way to quickly get at values of 
a hash when you provide an array of keys.

Remember when you deal with hash slices you need to address the
hash with an '@'.  This notation is what tripped me up when I 
first started working with them.

-biz-

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to