【问题标题】:Perl, loop through array of hashes and print a specific hash element based on criteriaPerl,遍历哈希数组并根据条件打印特定的哈希元素
【发布时间】:2021-08-19 22:36:29
【问题描述】:

我有一个更大的多维散列,其中包含散列和数组,我能够从仅对整个更大散列有用的散列数组中获取散列数组,现在我正在尝试打印一个特定的键的元素或值。

原始哈希

{
      "grade1": {
        "sections": {
          "groups": "group-a"
        }
      },
      "grade2": {
        "sections": {
          "groups": "group-b"
        }
      },
      "grade3": {
        "departments": [
          {
            "allowedsubjects": "\ngeneral\nmath\nphysics\nchemistry",
            "name": "class1",
            "multiple": false,
            "description": "",
            "required": "optional"
          },
          {
            "allowedsubjects": "\ngeneral\nbiology\nphysics\nchemistry",
            "name": "class2",
            "multiple": false,
            "description": "",
            "required": "optional"
          }
        ]
      }
    }

我的脚本

#!/usr/bin/perl
use Cpanel::JSON::XS;
use strict;
use v5.16.3;
use Data::Dumper;

my $json_text ='{ "grade1": { "sections": { "groups": "group-a" } }, "grade2": { "sections": { "groups": "group-b" } }, "grade3": { "departments": [ { "allowedsubjects": "\ngeneral\nmath\nphysics\nchemistry", "name": "class2", "multiple": false, "description": "", "required": "optional" }, { "allowedsubjects": "\ngeneral\nbiology\nphysics\nchemistry", "name": "class1", "multiple": false, "description": "", "required": "optional" } ] } }';

my $json_obj = decode_json($json_text);

#print Dumper $json_obj;
my %school = %{$json_obj};
## Half working
    my @AoH = @{$school{"grade3"}{"departments"}};
    my @lines;
    for my $href ( @AoH ) {
        for my $key ( keys %$href ) {
          push @lines, $href->{$key};
         }
    }
print @lines;

输出

0optionalclass2
general
math
physics
chemistryoptionalclass1
general
biology
physics
chemistry0

如何仅打印基于 class1 或 class2 作为标准的 allowedsubjects 的值。

选择 class1 时的预期输出

general
math
physics
chemistry

【问题讨论】:

    标签: arrays json perl hash


    【解决方案1】:

    让我们从避免不必要的哈希和数组副本开始。我们还将使用比AoHhrefkey 更好的变量名。

    my $school = decode_json($json);
    my $depts = $school->{grade3}{departments};
    

    现在我们想要名称值为$dept_name 的部门。 grep 是一个很好的过滤工具。

    my $dept_name = 'class1';
    my @matching_depts = grep { $_->{name} eq $dept_name } @$depts;
    

    然后,这只是一个迭代匹配部门并打印所需值的问题。

    for my $dept (@matching_depts) {
       say $dept->{allowedsubjects};
    }
    

    除了不完全。打印出来的

                     <-- Blank line
    general
    biology
    physics
    chemistry
    

    修复:

    for my $dept (@matching_depts) {
       say substr($dept->{allowedsubjects}, 1);
    }
    

    替代修复:

    for my $dept (@matching_depts) {
       my @subjects = grep { $_ ne "" } split /\n/, $dept->{allowedsubjects};
       for my $subject (@subjects) {
          say $subject;
       }
    }
    

    【讨论】:

      【解决方案2】:

      您可以在代码中进行相当多的简化。归结为这个循环。

      for my $department ( @{ $school->{grade3}{departments} } ) {
          if ($department->{name} eq 'class1') {
              print $department->{allowedsubjects};
          }
      }
      

      我已删除您的@lines,因为不需要存储此示例的输出。我还删除了@AoH,因为我们可以直接访问它。我已将$href 重命名为$department,这样更具描述性。

      然后归结为查看每个部门内的特定name 键,与您想要的类进行比较,然后直接打印allowedsubjects

      【讨论】:

        猜你喜欢
        • 2012-08-22
        • 2012-07-22
        • 1970-01-01
        • 1970-01-01
        • 2012-07-27
        • 1970-01-01
        • 2013-01-16
        • 2013-11-10
        • 2022-07-21
        相关资源
        最近更新 更多