vkk05
vkk05

Reputation: 3222

Altering the Output from Hash Values in Perl

I have 2 hashes (%hash1, %hash2) with certain values in it.

I want to manipulate the hash in order to print the output in a desired way.

Here is my script:

#!/usr/bin/perl

use strict; use warnings;

use Data::Dumper;

my %hash1 = (
          'Network=Europe,Node=S02,Sec=1' => [
                                               'Network=Europe,Node=S02,SecAnt=1'
                                             ],
          'Network=Europe,Node=S01,Sec=1' => [
                                               'Network=Europe,Node=S01,SecAnt=1'
                                             ],
          'Network=Europe,Node=S01,Sec=2' => [
                                               'Network=Europe,Node=S01,SecAnt=1'
                                             ]
);
my %hash2 = (
          'Network=Europe,Node=S01,Sec=2' => [
                                               'Network=Europe,Node=S01,Cell=1'
                                             ],
          'Network=Europe,Node=S01,Sec=1' => [
                                               'Network=Europe,Node=S01,Cell=3'
                                             ],
          'Network=Europe,Node=S02,Sec=1' => [
                                               'Network=Europe,Node=S02,Cell=1'
                                             ]
);

my %altered;

foreach my $sec (keys %hash1){
    foreach my $ant (@{$hash1{$sec}}) {
        push @{$altered{$ant}},$hash2{$sec};
    }
}

my $i = 0;

foreach my $sec_ant (sort keys %altered){
    ++$i;
    print "sec_ant_$i:$sec_ant\n";
    foreach my $val (@{$altered{$sec_ant}}){
        foreach my $cell (@$val){
            print "cell_$i:$cell\n"
        }
    }
}

Present output:

sec_ant_1:Network=Europe,Node=S01,SecAnt=1
cell_1:Network=Europe,Node=S01,Cell=1
cell_1:Network=Europe,Node=S01,Cell=3
sec_ant_2:Network=Europe,Node=S02,SecAnt=1
cell_2:Network=Europe,Node=S02,Cell=1

Expected/Desired Output:

sec_ant_1:Network=Europe,Node=S01,SecAnt=1
cell_1:Network=Europe,Node=S01,Cell=1###Network=Europe,Node=S01,Cell=3
sec_ant_2:Network=Europe,Node=S02,SecAnt=1
cell_2:Network=Europe,Node=S02,Cell=1

If the particular sec_ant_x have multiple cell_x values, it should concatenate it with the ### delimeter. Also, the cell_x values should be unique. How can I do that?

Upvotes: 2

Views: 53

Answers (1)

toolic
toolic

Reputation: 62236

I modified the way you create your altered data structure; I dereference the array before storing it. This simplifies retrieval.

Then you can join the elements of the array with ### when you print:

my %altered;

foreach my $sec (keys %hash1){
    foreach my $ant (@{$hash1{$sec}}) {
        push @{$altered{$ant}}, @{$hash2{$sec}};
    }
}

my $i = 0;

foreach my $sec_ant (sort keys %altered){
    ++$i;
    print "sec_ant_$i:$sec_ant\n";
    print "cell_$i:", join('###', @{$altered{$sec_ant}}), "\n";
}

Prints:

sec_ant_1:Network=Europe,Node=S01,SecAnt=1
cell_1:Network=Europe,Node=S01,Cell=1###Network=Europe,Node=S01,Cell=3
sec_ant_2:Network=Europe,Node=S02,SecAnt=1
cell_2:Network=Europe,Node=S02,Cell=1

Upvotes: 3

Related Questions