Hash content extraction based on condition

后端 未结 1 793
北荒
北荒 2021-01-27 03:19

I have a hash containing node data.

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

Here i

相关标签:
1条回答
  • 2021-01-27 03:53

    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;
       }
    }
    

    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.


    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;
       }
    }
    

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

    0 讨论(0)
提交回复
热议问题