根据条件提取哈希含量 [英] Hash content extraction based on condition

查看:53
本文介绍了根据条件提取哈希含量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含节点数据的哈希.

I have a hash containing node data.

我希望将散列内容打印在 -r_< count> -d_< count> 属性中.

I am expecting hash content to be printed in -r_<count> and -d_<count> attributes.

这是脚本:

use strict; use warnings;

use Data::Dumper;

my %hash = (
    'Network=Test,Cell=31' => [ 'Network=Test,Unit=RU-1-1,Port=A',
                                'Network=Test,Unit=RU-1-2,Port=A'
                              ],
    'Network=Test,Cell=32' => [ 'Network=Test,Unit=RU-1-1,Port=A',
                                'Network=Test,Unit=RU-1-2,Port=A'
                              ],
    'Network=Test,Cell=33' => [ 'Network=Test,Unit=RU-1-5,Port=A',
                                'Network=Test,Unit=RU-1-6,Port=A'
                              ],
);

print "hash:\n".Dumper(\%hash);

my $count = 0;

foreach my $d (sort keys %hash) { 
    $count++;
    print "-d_". $count."=".$d . "\n";
    
    my %seen = ();
    foreach my $r (sort @{$hash{$d}}) {
        $seen{$r}++;
    }

    if ((keys %seen) > 0) {
        my $uniq = join ("###",sort keys %seen);
        print "-r_". $count . "=" . $uniq . "\n";
    } else {
        print "-r_". $count."="."NA\n";
    }
}

我能够打印如下输出(当前输出):

And I am able to print output like below(current output):

-d_1=Network=Test,Cell=31
-r_1=Network=Test,Unit=RU-1-1,Port=A###Network=Test,Unit=RU-1-2,Port=A
-d_2=Network=Test,Cell=32
-r_2=Network=Test,Unit=RU-1-1,Port=A###Network=Test,Unit=RU-1-2,Port=A
-d_3=Network=Test,Cell=33
-r_3=Network=Test,Unit=RU-1-5,Port=A###Network=Test,Unit=RU-1-6,Port=A

但是我希望输出打印如下(预期输出):

But I want output to be printed like below (expected output):

-r_1=Network=Test,Unit=RU-1-1,Port=A
-d_1=Network=Test,Cell=31###Network=Test,Cell=32
-r_2=Network=Test,Unit=RU-1-2,Port=A
-d_2=Network=Test,Cell=31###Network=Test,Cell=32
-r_3=Network=Test,Unit=RU-1-5,Port=A
-d_3=Network=Test,Cell=33
-r_4=Network=Test,Unit=RU-1-6,Port=A
-d_4=Network=Test,Cell=33

预期的输出是, -r_< count> 的值应打印为单数(来自%hash 键数组值)和 -d_ <计数> (来自%hash 键).

The expected output is, the value of -r_<count> should be printed as singular (from %hash keys array value) and -d_<count> (from %hash keys) should printed.

推荐答案

输出由数组的唯一值指导.因此,您的输出循环必须在这些循环上进行迭代.

The output is guided by the unique values of the arrays. Your output loop must therefore iterate over these.

(
   'Network=Test,Unit=RU-1-1,Port=A',
   'Network=Test,Unit=RU-1-2,Port=A',
   'Network=Test,Unit=RU-1-5,Port=A',
   'Network=Test,Unit=RU-1-6,Port=A',
)

但是,对于每个这些,输出都需要关联的键.这意味着输出循环需要以下数据:

However, for each of these, the output needs the associated keys. This means the output loop requires the following data:

(
   'Network=Test,Unit=RU-1-1,Port=A' => [ 'Network=Test,Cell=31', 'Network=Test,Cell=32' ],
   'Network=Test,Unit=RU-1-2,Port=A' => [ 'Network=Test,Cell=31', 'Network=Test,Cell=32' ],
   'Network=Test,Unit=RU-1-5,Port=A' => [ 'Network=Test,Cell=33' ],
   'Network=Test,Unit=RU-1-6,Port=A' => [ 'Network=Test,Cell=33' ],
)

基本上,您的数据结构是由内而外的.但是,既然我们知道了我们想要的是什么,这仅仅是将数据结构转换为我们所需要的一个问题.

Basically, your data structure is inside-out. But now that we know what we want, it's just a question of transforming the data structure into what we need.

my %foos_by_bar;
for my $foo (keys %hash) {   # %hash_b
   my $bars = $hash{$foo};   # %hash_a

   for my $bar (@$bars) {
      push @{ $foos_by_bar{$bar} }, $foo;
   }
}

输出循环仅需要遍历%foos_by_bar 的(可能是已排序的)键,并且 @ {$ foos_by_bar {$ bar}} 包含您需要的数据表示 -d .

The output loop simply needs to iterate over the (possibly sorted) keys of %foos_by_bar, and @{ $foos_by_bar{$bar} } contains the data you need for -d.

没有什么可以阻止您遍历输出循环中%foos_by_bar 的排序键以产生可预测的输出,但这不一定会给您与问题中相同的顺序.如果您需要该特定订单,则可以使用以下命令:

Nothing's stopping you from iterating over the sorted keys of %foos_by_bar in the output loop to produce predictable output, but that won't necessarily give you the same order as in the question. If you need that specific order, you can use the following:

my @bars;
my %foos_by_bar;
for my $foo (sort keys %hash) {   # %hash_b
   my $bars = $hash{$foo};        # %hash_a

   for my $bar (@$bars) {
      push @bars, $bar if !$foos_by_bar{$bar};
      push @{ $foos_by_bar{$bar} }, $foo;
   }
}

在这种情况下,输出循环将遍历 @bars .

In this case, the output loop would iterate over @bars.

这篇关于根据条件提取哈希含量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆