计算数组哈希值中的重复值

时间:2021-04-03 21:36:35

I'm generating a hash and pushed that hash in an array. The array at the end shows like

我正在生成一个哈希并将该哈希推送到一个数组中。最后的数组显示为

$VAR1 = [
          {
            'Count' => 1,
            'maint_wf' => 'WFblabla',
            'lines' => {
                         'line1' => {
                                      'ort' => 'city_x',
                                      'lbz' => 'L1city_x'
                                    }
                       },
            'receive_date' => 'Di, 3 Sep 2013 12:16:43 +0200',
            'maint_date' => '02.09.2013',
            'calendar' => {
                            'dtend' => '20130902T0530',
                            'dtstart' => '20130902T0100'
                          }
          },
          {
            'Count' => 3,
            'maint_wf' => 'WFbla',
            'lines' => {
                         'line3' => {
                                      'ort' => 'city1',
                                      'lbz' => 'L1_city1'
                                    },
                         'line1' => {
                                      'ort' => 'city2',
                                      'lbz' => 'L1_city2'
                                    },
                         'line2' => {
                                      'ort' => 'city2',
                                      'lbz' => 'L2_city2'
                                    }
                       },
            'receive_date' => 'Mi, 4 Sep 2013 08:56:35 +0200',
            'maint_date' => '03.09.2013',
            'calendar' => {
                            'dtend' => '20130903T0530',
                            'dtstart' => '20130903T0300'
                          }
          },
          ...
        ];

How can I count the duplicate values of the key 'ort' (i.e. how many 'ort' => 'city2' exist?) and then just display the corresponding values of key 'lbz'?

如何计算键'ort'的重复值(即有多少'ort'=>'city2'?)然后只显示键'lbz'的相应值?

2 个解决方案

#1


1  

Creating a hash that counts the possible lbz values for each ort value might help you:

创建一个计算每个ort值的可能lbz值的哈希可能会帮助您:

#!/usr/bin/perl
use warnings;
use strict;

use Data::Dumper;

my $arrR = [
            {
                'Count' => 1,
       # ...  
           ];

my %ort;
for my $hashR (@$arrR) {
    my @lines = values %{ $hashR->{lines} };
    for my $line (@lines) {
        $ort{ $line->{ort} }{ $line->{lbz} }++;
    }
}
print Dumper \%ort;

Output:

$VAR1 = {
          'city2' => {
                       'L1_city2' => 1,
                       'L2_city2' => 1
                     },
          'city_x' => {
                        'L1city_x' => 1
                      },
          'city1' => {
                       'L1_city1' => 1
                     }
        };

#2


1  

my $wanted = "city2";

my @lines;
for my $h (@$arr) {
  push @lines, grep { $_->{ort} eq $wanted } values %{$h->{lines}};
}

print "Count:", scalar @lines, "\n";
print $_->{lbz}, "\n" for @lines;

Perhaps shorter, but significantly more obscure and not recommended:

也许更短,但更明显更模糊,不推荐:

my $wanted = "city2";

printf "Count:%d\n", scalar
  map { $_->{ort} eq $wanted ? print "$_->{lbz}\n" : () }
  map { values %{$_->{lines}} } 
  @$arr;

#1


1  

Creating a hash that counts the possible lbz values for each ort value might help you:

创建一个计算每个ort值的可能lbz值的哈希可能会帮助您:

#!/usr/bin/perl
use warnings;
use strict;

use Data::Dumper;

my $arrR = [
            {
                'Count' => 1,
       # ...  
           ];

my %ort;
for my $hashR (@$arrR) {
    my @lines = values %{ $hashR->{lines} };
    for my $line (@lines) {
        $ort{ $line->{ort} }{ $line->{lbz} }++;
    }
}
print Dumper \%ort;

Output:

$VAR1 = {
          'city2' => {
                       'L1_city2' => 1,
                       'L2_city2' => 1
                     },
          'city_x' => {
                        'L1city_x' => 1
                      },
          'city1' => {
                       'L1_city1' => 1
                     }
        };

#2


1  

my $wanted = "city2";

my @lines;
for my $h (@$arr) {
  push @lines, grep { $_->{ort} eq $wanted } values %{$h->{lines}};
}

print "Count:", scalar @lines, "\n";
print $_->{lbz}, "\n" for @lines;

Perhaps shorter, but significantly more obscure and not recommended:

也许更短,但更明显更模糊,不推荐:

my $wanted = "city2";

printf "Count:%d\n", scalar
  map { $_->{ort} eq $wanted ? print "$_->{lbz}\n" : () }
  map { values %{$_->{lines}} } 
  @$arr;