TASdvlper has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I have two arrays of equal sizes, is there a way I can use one array for the "keys" of a hash and the other array the "values" of a hash (assuming 1:1 mapping) ?

@array1 = qq (foo, foo1, foo2, foo3, foo4, ...) @array2 = qq (bar, bar1, bar2, bar3, bar4, ...).
and I want the hash to look like:
$myhash{'foo'} = 'bar'; $myhash{'foo1'} = 'bar1'; $myhash{'foo2'} = 'bar2'; $myhash{'foo3'} = 'bar3'; $myhash{'foo4'} = 'bar4';
Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: Populating a hash from array
by jeffa (Bishop) on Jan 05, 2004 at 21:36 UTC
    This is known as a hash slice:
    my %hash; @hash{@array1} = @array2;
    The key is to declare the hash first, then apply the slice.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      thanks ... I remember reading about hash slices but, until now, never had a need to use them.
Re: Populating a hash from array
by duff (Parson) on Jan 05, 2004 at 21:36 UTC

    @hash{@keys} = @values;

Re: Populating a hash from array
by Abigail-II (Bishop) on Jan 05, 2004 at 21:36 UTC
    my %myhash; @myhash {@array1} = @array2;

    Abigail

Re: Populating a hash from array
by bart (Canon) on Jan 05, 2004 at 22:03 UTC
    @array1 = qq (foo, foo1, foo2, foo3, foo4, ...) @array2 = qq (bar, bar1, bar2, bar3, bar4, ...).
    As a sidenote, you don't want qq, you want qw. And get rid of the commas.
    @array1 = qw(foo foo1 foo2 foo3 foo4 ...); @array2 = qw(bar bar1 bar2 bar3 bar4 ...);
      thanks ... probably just a typo on my part.
        TASdvlper,
        In addition to what bart said, if your data really does look like this - there is probably a much better way to build your array (the lazy way).
        my @array1 = map { 'foo' . $_ } 1 .. 99;
        "Ok great, but I want the first element to just be 'foo' with no number"
        my @array1 = map { $_ ? 'foo' . $_ : 'foo' } 0 .. 99;
        Cheers - L~R
Re: Populating a hash from array
by Roger (Parson) on Jan 05, 2004 at 22:36 UTC
    Hash slice is (usually) the way to go.

    You can also use the map function to build the hash, as illustrated by the following example:
    my %myhash = map { $array1[$_] => $array2[$_] } 0 .. $#array1;

Re: Populating a hash from array
by ryantate (Friar) on Jan 05, 2004 at 21:55 UTC

    I was just thinking about hash slices the other day, and wondering: what is the syntax for slicing a hash -reference-? I always end up doing things like

    ($hashref->{key1}, $hashref->{key2}) = @values;

    or

    {map {$keynames[$_]=>$values[$_]} 0 .. $#values};

    which seems much less elegant.

      what is the syntax for slicing a hash -reference-?
      @{$href}{@keys}
        If the reference is a simple scalar, you can just say @$href{@keys}. The curly braces around the reference are only necessary if the reference is a more complicated expression (e.g. @{gimme_href()}{@keys}).

        Same thing goes with using a slice or $# (last index) on an array reference: @$aref[@indices] and $#$aref or @{gimme_aref()}[@indices] and $#{gimme_aref()}.

        Of course, if the curlies make it more clear to you, by all means use them.

Re: Populating a hash from array
by Anonymous Monk on Jan 06, 2004 at 18:55 UTC
    %myhash = map { $array1[$_] => $array2[$_] } ( 0 .. $#array1);