in reply to Populating a hash-ref from multiple arrays with slice?

Slices would not be useful for this problem.

Here is the equivalent of toolic's solution, in one line:

my $h={map{$a1[$_]=>{Metadata=>$a2[$_],Submitted=>$a3[$_]}}0..$#a1};

        "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Replies are listed 'Best First'.
Re^2: Populating a hash-ref from multiple arrays with slice?
by FreeBeerReekingMonk (Deacon) on Apr 16, 2015 at 20:45 UTC

    But if the OP wants it as slice, this is the code:

    @array1 = qw(some_unique_key1 some_unique_key2 some_unique_key3); @array2 = qw(meta_data_1 metadata_2 metadat_3); @array3 = qw(submitted_date1 submitted_date2 submitted_date3); my $hashref; # @hash{@keys} = @values; @$hashref{@array1} = map { {'Metadata' =>$array2[$_], 'Submitted Date' +=>$array3[$_]} } 0..$#array1; use Data::Dumper; die Dumper $hashref;

    It would then give this as output:

    $VAR1 = { 'some_unique_key3' => { 'Submitted Date' => 'submitted_date3', 'Metadata' => 'metadat_3' }, 'some_unique_key2' => { 'Submitted Date' => 'submitted_date2', 'Metadata' => 'metadata_2' }, 'some_unique_key1' => { 'Submitted Date' => 'submitted_date1', 'Metadata' => 'meta_data_1' } };

      Rate netWallah hashref toolic jeffa netWallah 324041/s -- -16% -16% -35% hashref 387028/s 19% -- -0% -22% toolic 387777/s 20% 0% -- -22% jeffa 497471/s 54% 29% 28% --
      Note that Jeffa's code is faster because it uses HoA, and not HoH. if you then use constants to access 0 and 1 it could be the way to go.