【问题标题】:How to access array of hashes?如何访问哈希数组?
【发布时间】:2013-12-04 02:10:38
【问题描述】:

您好,我有一个如下的哈希数组,我想访问哈希元素/元素。假设我想打印 doct1 的名字,我没有得到正确的结果请帮助我如何打印?

@doctors = (
    'doct1' => {
          'name'            => 'abcd',
          'specialization'  => 'xyz',
          'city'            => 'pqr'
          },
    'doct2' => {
          'name'            => 'efgh',
          'specialization' => 'mno',
          'city'            => 'stu'
          }
);
print $doctors[0]{'name'};

【问题讨论】:

  • 那不是哈希数组,而是哈希哈希。
  • 其实都不是
  • string,href,string,href数组
  • 如果您将@doctors 更改为%doctors,则LHS 与预期 RHS 一致,并且您有一个哈希值。
  • 这是一个数组,其中第一个和第三个元素是字符串,第二个和第四个元素是 hashrefs。很清楚:D

标签: perl


【解决方案1】:

数组没有键,

my @doctors = (
         {
          'name'            => 'abcd',
          'specialization'  => 'xyz',
          'city'            => 'pqr'
          },
         {
          'name'            => 'efgh',
          'specialization' => 'mno',
          'city'            => 'stu'
          }
);
print $doctors[0]{'name'};

【讨论】:

    【解决方案2】:

    你没有 AoH。您有一个包含字符串和对哈希的引用的数组。这是一个非常糟糕的数据结构。找到正确的医生既麻烦又低效。

    my $i = 0;
    $i += 2 while $i<@doctors && $doctors[$i] ne 'doct1';
    die "Not found" if $i > @doctors;
    say $doctors[$i+1]{name};
    

    如果你有你所说的 AoH,你看起来像这样:

    my @doctors = (
       {
          id             => 'doct1',
          name           => 'abcd',
          specialization => 'xyz',
          city           => 'pqr',
       },
       {
          id             => 'doct2',
          name           => 'efgh',
          specialization => 'mno',
          city           => 'stu',
       },
    );
    

    这样会更好。

    my ($doctor) = grep { $_->{id} eq 'doct1' } @doctors
       or die "Not found";
    say $doctor->{name};
    

    doct1doct2 也可能毫无意义,而您会很乐意使用 01 代替。如果是这样,

    die "Not found" if @doctors < 0;
    say $doctors[0]{name};
    

    如果doct1doct2 并非毫无意义,那么最干净、最有效的解决方案就是使用 HoH。

    my %doctors = (
       doct1 => {
          name           => 'abcd',
          specialization => 'xyz',
          city           => 'pqr',
       },
       doct2 => {
          name           => 'efgh',
          specialization => 'mno',
          city           => 'stu',
       },
    );
    

    那么代码就很简单了:

    my $doctor = $doctors{doct1}
       or die "Not found";
    say $doctor->{name};
    

    【讨论】:

      【解决方案3】:

      在这种情况下,使用Data::Dumper 是必不可少的,您实际上拥有的是一个由两个字符串和两个哈希引用组成的数组。如果你用 Data::Dumper 打印出来,你会看到:

      use Data::Dumper;
      print Dumper \@doctors;
      [
         'doct1',
         {
           'city' => 'pqr',
           'specialization' => 'xyz',
           'name' => 'abcd'
         },
         'doct2',
         {
           'city' => 'stu',
           'specialization' => 'mno',
           'name' => 'efgh'
         }
      ];
      

      每个 hashref 都有代表医生的所有数据,前面的附加键没有任何意义。删除这些键,您将拥有如下结构:

      @doctors = (
                {
                 'name'            => 'abcd',
                 'specialization'  => 'xyz',
                 'city'            => 'pqr'
                },
                {
                 'name'            => 'efgh',
                 'specialization'  => 'mno',
                 'city'            => 'stu'
                }
      );
      

      现在您可以像预期的那样访问哈希属性:

      print $doctors[0]{name};
      

      【讨论】:

        【解决方案4】:

        右边的声明与对数组的赋值不是很一致(在意图上)。您可能希望将其分配给哈希:

        %doctors = (
            'doct1' => {
                  'name'            => 'abcd',
                  'specialization'  => 'xyz',
                  'city'            => 'pqr'
                  },
            'doct2' => {
                  'name'            => 'efgh',
                  'specialization' => 'mno',
                  'city'            => 'stu'
                  }
        );
        
        print $doctors{'doct1'}->{'name'};
        

        要么这个,要么,mpapec 的答案。

        【讨论】:

          猜你喜欢
          • 2015-06-26
          • 2013-03-22
          • 2013-08-21
          • 2013-03-08
          • 2012-05-14
          • 1970-01-01
          • 2021-01-18
          • 1970-01-01
          • 2014-05-06
          相关资源
          最近更新 更多