3

How can I merge two arrays together as an associative manner; one array having the key column names, and the other the values?

I have tried to push one array upon the other, put only appends them as a list, does not associate them together. Any help would be greatly appreciated it. Thanks!

my @var1 = {'COL1', 'COL2', 'COL3'};
my @var2 = {  '1' ,  '2'  , '3'   };

...

new array %var3 = {'COL1' => '1', 'COL2' => '2', 'COL3' => '3'} 
Filip Roséen - refp
  • 60,448
  • 19
  • 148
  • 192

2 Answers2

3

With hash slices:

my %var3;
@var3{ @var1 } = @var2;
Jim Davis
  • 4,838
  • 1
  • 25
  • 22
3

First some comments. Arrays use simple parentheses ( and ).

And you can construct the hash with a hash slice:

my @keys = ('COL1', 'COL2', 'COL3');
my @values = ( '1' , '2' , '3' );

my %hash ;
@hash{@keys} = @values ;

This gives the desired hash you wanted.

Borodin
  • 125,056
  • 9
  • 69
  • 143
dgw
  • 12,645
  • 11
  • 55
  • 51