sun@icgeb has asked for the wisdom of the Perl Monks concerning the following question:

Hi! This is my first post.I have two arrays,one contains keys,as in @Arrayone = ('A','B','C','D') and second values @Arraytwo = ('1','2','3','4').I want to take values of first array as keys and that of second as values and create a hash %hash = ('A' -> '1', 'B' ->'2',.....).How can I do it? These are big arrays and not as simple as I have shown. Please reply!!
  • Comment on How to make a hash out of two arrays,one with keys and other with values?

Replies are listed 'Best First'.
Re: How to make a hash out of two arrays,one with keys and other with values?
by ikegami (Patriarch) on Jun 30, 2009 at 05:46 UTC

    Very simply:

    my %hash; @hash{@Arrayone} = @Arraytwo;

    See hash slices in perldata.

    You could also do it in one statement with the help of map.

    my %hash = map { $Arrayone[$_] => $Arraytwo[$_] } 0..$#Arrayone;
      And, as is often the case, less ops mean more win:
      #!/usr/bin/env perl use Benchmark 'cmpthese'; @k = 1..1000; @v = 1..1000; cmpthese(0, { map => 'my %h1 = map { $k[$_], $v[$_] } 0..$#k', for => 'my %h2; $h2{$k[$_]} = $v[$_] for 0..$#k', slice => 'my %h3; @h3{@k} = @v', }); __END__ Rate map for slice map 657/s -- -44% -65% for 1174/s 79% -- -38% slice 1884/s 187% 60% --
Re: How to make a hash out of two arrays,one with keys and other with values?
by jlk (Hermit) on Jun 30, 2009 at 05:39 UTC

    Ok, assuming that the arrays will be mapped element for element without mixing and matching, you can do something like this:

    use strict; use warnings; my @array1 = ('A','B','C','D'); my @array2 = ('1','2','3','4'); my %hash1; foreach my $key (@array1){ $hash1{$key} = shift(@array2); } for my $keys ( sort(keys %hash1 )) { my $value = $hash1{$keys}; print "$keys => $value\n"; }
    Hope this helped. Regards, Jeff

    "Every time Linux boots, a penguin gets its wings"

      I have this hash and I need to normalise it.But problem is that it is taking the value of first key i.e. '' as minimum value.I want to delete this first key-value pair.How do I go about it?Please help!! $VAR1 = { '' => undef, 'D ' => '4.76', 'S ' => '4.50', 'I ' => '3.95', 'T ' => '4.35', 'K ' => '4.36', 'C ' => '4.65', 'P ' => '4.44', 'Y ' => '4.60', 'R ' => '4.38', 'E ' => '4.29', 'V ' => '3.95', 'N ' => '4.75', 'H ' => '4.63', 'G ' => '3.97', 'Q ' => '4.37', 'M ' => '4.52', 'F ' => '4.66', 'L ' => '4.17', 'A ' => '4.35', 'W ' => '4.70' }; Thanks in advance!!
Re: How to make a hash out of two arrays,one with keys and other with values?
by dpetrov (Acolyte) on Jun 30, 2009 at 08:59 UTC
    Or you can use map and shift, like:
    map { $hash{ $_ } = shift @array2 } @array1;
Re: How to make a hash out of two arrays,one with keys and other with values?
by hnd (Scribe) on Jun 30, 2009 at 06:29 UTC
    mapping is the best..... since you have large arrays
    use strict; my %hash=map {@array1[$_]=>@array2[$_]} 0..$#array1;
      No - that does not work.

      You need =>     , not =

      . Also, the preferred sigil for array element in perl5 is $.

      my %hash=map {$array1[$_]=>$array2[$_]} 0..$#array1;
      (Identical to ikegami's posting.)

           Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)

        really sorry.........
        i overlooked it.....
Re: How to make a hash out of two arrays,one with keys and other with values?
by wardmw (Acolyte) on Jun 30, 2009 at 08:16 UTC
    This works:
    my @Arrayone = ('A','B','C','D'); my @Arraytwo = ('1','2','3','4'); my (%Arraythree, $ct); for ($ct=0; $ct <= $#Arrayone; $ct++) { $Arraythree{$Arrayone[$ct]} = $Arraytwo[$ct]; }
    This assumes that keys array and values array match up.

    |\/|artin

Re: How to make a hash out of two arrays,one with keys and other with values?
by bichonfrise74 (Vicar) on Jul 01, 2009 at 01:49 UTC
    Another way...
    #!/usr/bin/perl use strict; my %hash; my @one = qw( a b c d ); my @two = qw( 1 2 3 4 ); for (0 .. $#one) { $hash{$one[$_]} = $two[$_]; }