【问题标题】:Is there any way to create a hash with two arrays in perl?有没有办法在 perl 中创建一个包含两个数组的哈希?
【发布时间】:2013-03-02 00:42:48
【问题描述】:

我是 Perl 的新手,所以我遇到了一些麻烦。 假设我有两个数组:

@num = qw(one two three);
@alpha = qw(A B C);
@place = qw(first second third);

我想创建一个散列,其中第一个元素作为键,其余作为值作为数组,无论它们有 3 个还是 3000 个元素

所以哈希本质上是这样的:

%hash=(
    one => ['A', 'first'],
    two => ['B', 'second'],
    third => ['C', 'third'],
);

【问题讨论】:

  • 如果两个数组的第一个元素相同怎么办?
  • @DavidO 我已经尝试过foreach 循环,但这真的很混乱

标签: arrays perl hash


【解决方案1】:
use strict;
use warnings;

my @num   = qw(one two three);
my @alpha = qw(A B C);
my @place = qw(first second third);

my %hash;
while (@num and @alpha and @place) {
  $hash{shift @num} = [ shift @alpha, shift @place ];
}

use Data::Dump;
dd \%hash;

输出

{ one => ["A", "first"], three => ["C", "third"], two => ["B", "second"] }

【讨论】:

    【解决方案2】:
    use strict;
    use warnings;
    use Data::Dumper;
    
    my %hash;
    my @num   = qw(one two three);
    my @alpha = qw(A B C);
    my @place = qw(first second third);
    
    $hash{ $num[$_] } = [ $alpha[$_], $place[$_] ] for 0 .. $#num;
    
    print Dumper \%hash
    

    输出:

    $VAR1 = {
              'three' => [
                           'C',
                           'third'
                         ],
              'one' => [
                         'A',
                         'first'
                       ],
              'two' => [
                         'B',
                         'second'
                       ]
            };
    

    【讨论】:

      【解决方案3】:
      use strict;
      use warnings;
      use Algorithm::Loops 'MapCarE';
      
      my @num = qw(one two three);
      my @alpha = qw(A B C);
      my @place = qw(first second third);
      
      my %hash = MapCarE { shift, \@_ } \@num, \@alpha, \@place;
      

      【讨论】:

        【解决方案4】:
        use strict; use warnings;
        my @num = qw(one two three);
        my @alpha = qw(A B C);
        my @place = qw(first second third);
        
        my %h;
        push @{ $h{$num[$_]} }, $alpha[$_], $place[$_] for 0..$#num;
        
        use Data::Dumper;
        print Dumper \%h;
        

        输出

        $VAR1 = {
                  'three' => [
                               'C',
                               'third'
                             ],
                  'one' => [
                             'A',
                             'first'
                           ],
                  'two' => [
                             'B',
                             'second'
                           ]
                };
        

        【讨论】:

          【解决方案5】:
          use List::UtilsBy qw( zip_by );
          
          my @num = qw(one two three);
          my @alpha = qw(A B C);
          my @place = qw(first second third);
          
          my %hash = zip_by { shift, [ @_ ] } \@num, \@alpha, \@place;
          

          输出:

          $VAR1 = {
                'three' => [
                             'C',
                             'third'
                           ],
                'one' => [
                           'A',
                           'first'
                         ],
                'two' => [
                           'B',
                           'second'
                         ]
              };
          

          【讨论】:

            猜你喜欢
            • 2011-05-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-06-14
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多