0

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'?

0

2 Answers 2

1

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

#!/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
                     }
        };
Sign up to request clarification or add additional context in comments.

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.