jcwren beat me to it, but I was proud I knew this so I'm leaving it up : P
use strict;
my @a = qw(a b c d e f);
my @n = qw(1 2 3 4 5 6);
my %hash;
@hash{@a} = @n;
print "$hash{a} $hash{e}\n\n";
outputs:
jptxs:/home/jptxs $ perl -w 2arrayHash
1 5
jptxs:/home/jptxs $
basically @hash tells perl to treat the hash as an array which means feeding it keys creates a slice just like @array[5,8,12] might. this assumes that you have two arrays of the same length, though. are you sure you do?
Update: the ever wise and vigilent chromatic has reminded me that a slice is a list and not an array - meaning it functions like something which is returned in list context, not like a set of values stored in and array. you could set that list equal to an array to access it later, but that is not what happens automatically. So don't expect to take a slice like @array[4..7] or @hash{@a} and find the value neatly tucked away anywhere for your use unless you make my @array_from_slice = @hash{@a};
"sometimes when you make a request for the head you don't
want the big, fat body...don't you go snickering."
-- Nathan Torkington UoP2K a.k.a gnat
|