Create a hash in which the key is an array and the value is an array of arrays

I am trying to create a hash in which the keys are in the array and the values ​​in the array are:

my @keys = (1,2,3,4,5); my @value1 = (a,b,c,d,e); my @value2 = (f,g,h,i,j); my @value3 = (k,l,m,n,o); my @values = ([@value1],[@value2],[@value3]); my %hash; 

I want to create a hash with @keys as keys and @values ​​as values, so that key "1" returns the values ​​a, f, k (0th element in each array) and so on.

For one key, this will be achieved as follows:

 %hash=('key'=>@values); 

But I'm not sure how to change this for an array of keys.

Any help would be awesome!

Greetings

N

+4
source share
5 answers

I use the syntax $foo[$i][$j]; to represent an array of arrays as a two-dimensional array. Here the answer will not be map :

 #! /usr/bin/env perl use 5.12.0; use warnings; use Data::Dumper; my @keys = qw(alpha beta gamma delta epsolon); my @values1 = qw(one two three four five); my @values2 = qw(uno dos tres quatro cinco); my @values3 = qw(abcde); my @values = ( \@values1, \@values2, \@values3 ); my %hash; for my $item ( (0..$#keys) ) { my @array; push @array, $values[0][$item], $values[1][$item], $values[2][$item]; $hash{$keys[$item]} = \@array; } say Dumper \%hash; 

Here's the conclusion:

 $VAR1 = { 'gamma' => [ 'three', 'tres', 'c' ], 'delta' => [ 'four', 'quatro', 'd' ], 'alpha' => [ 'one', 'uno', 'a' ], 'beta' => [ 'two', 'dos', 'b' ], 'epsolon' => [ 'five', 'cinco', 'e' ] }; 

It looks right. Of course, I never checked that the different arrays are the same size.

+2
source

Something like that:

 my %hash = map { $keys[$_] => [ $value1[$_], $value2[$_], $value3[$_] ] } 0..$#keys; 

assuming all four lists are the same length.

+4
source

Try it...

  #!  / usr / bin / perl
 use strict;
 use warnings;
 use Data :: Dumper;

 my @keys = qw (undef 0 1 $ key kappa);
 my @ value1 = qw (abcde);
 my @ value2 = qw (fghij);
 my @ value3 = qw (klmno);

 # not used here ...
 # my @values ​​= ([@ value1], [@ value2], [@ value3]);

 my% hash = map {my $ key = "$ keys [$ _]";  $ key => [$ value1 [$ _], $ value2 [$ _], $ value3 [$ _]]} (0 .. $ # keys);

 for my $ key (sort keys% hash) {
     print "Key: $ key contains:";
     for my $ value (@ {$ hash {$ key}}) {
         print "$ value";
     }
     print "\ n";
 }

 print "Should print 'c':" .@ {$ hash {'1'}} [0]. "\ n";
 print "Should print 'j':" .@ {$ hash {'kappa'}} [1]. "\ n";

 # print Dumper (% hash);

With the expected output as follows:

  Key: $ key contains: din
 Key: 0 contains: bgl
 Key: 1 contains: chm
 Key: kappa contains: ejo
 Key: undef contains: afk
 Should print 'c': c
 Should print 'j': j

Addendum: if you want to access a single value in a hash, it must be surrounded by @ {} in order to convert the reference to an anonymous array, and then end your index (starting from zero) in square brackets, for example [0]. Examples:

  print "Should print 'c':" .@ {$ hash {'1'}} [0]. "\ n";
 print "Should print 'j':" .@ {$ hash {'kappa'}} [1]. "\ n";

Modified to include Ekkehard in a more proper use (0 .. $ # ..) and added some bullet key checks.

+2
source

A combination of the best Vedran and Jim solutions:

 use strict; use warnings; # my @keys = ( 1, 2, 3, 4, 5); my @keys = ( 'alpha','beta','gamma','delta','epsilon'); my @value1 = ('a','b','c','d','e'); my @value2 = ('f','g','h','i','j'); my @value3 = ('k','l','m','n','o'); my %hash = map { $keys[$_] => [ $value1[$_], $value2[$_], $value3[$_] ] } (0 .. $#keys); printf 'first (%s) value: [%s]', $keys[0], join ", ", @{$hash{$keys[0]}}; 

output:

 first (alpha) value: [a, f, k] 

or

 first (1) value: [a, f, k] 

depending on which @keys you choose.

+1
source
 use Algorithm::Loops 'MapCarE'; my @keys = qw(1 2 3 4 5); my @value1 = qw(abcde); my @value2 = qw(fghij); my @value3 = qw(klmno); my %hash = MapCarE { $_[0] => [ @_[1..$#_] ] } \(@keys, @value1, @value2, @value3); 

MapCarE goes through the arrays, calling the code you supply, first passing the first elements of the arrays, then the seconds element, etc.

0
source

Source: https://habr.com/ru/post/1482162/


All Articles