I have data which need to be parsed and print on the screen.
Here is the data and my script:
Input Data:
__DATA__
Node : Network=Europe,Network=Ireland,Node=S01,Sec=1
Ref : [Network=Europe,Network=Ireland,Node=S01,SecAnt=1]
Node : Network=Europe,Network=Ireland,Node=S01,Sec=1,Car=1
resBy : [Network=Europe,Network=Ireland,Node=S01,Cell=1]
Node : Network=Europe,Network=Ireland,Node=S01,Sec=1,Car=2
resBy : [Network=Europe,Network=Ireland,Node=S01,Cell=1]
Script:
#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper;
use List::Util qw /uniq/;
my ($node, $item, %hash1, %hash2, %hash3);
while (my $line = <DATA>){
chomp $line;
if ($line =~ m/^\s*Node :\s*(.*?)(,Car=\d+)?\s*$/) {
$node = $1;
} elsif ($line =~ m/^\s*Ref :\s*\[*(.*?)\]*\s*$/) {
$hash1{$node} = [ split /, /,$1 ] ;
($node, $item) = "";
} elsif ($line =~ m/^\s*resBy :\s*\[*(.*?)\]*\s*$/) {
$hash2{$node} = [ split /, /,$1 ];
($node, $item) = "";
}
}
foreach my $sec (keys %hash1){
foreach my $ant (@{$hash1{$sec}}) {
push @{$hash3{$ant}}, $sec;
}
}
print "Hash3:\n". Dumper(\%hash3);
my $i = 0;
foreach my $sec_ant (sort keys %hash3){
++$i;
print "sec_ant_$i:$sec_ant\n";
print "car_$i:", join('###', uniq @{$hash3{$sec_ant}}), "\n";
}
This is my current output:
Hash3:
$VAR1 = {
'Network=Europe,Network=Ireland,Node=S01,SecAnt=1' => [
'Network=Europe,Network=Ireland,Node=S01,Sec=1'
]
};
sec_ant_1:Network=Europe,Network=Ireland,Node=S01,SecAnt=1
car_1:Network=Europe,Network=Ireland,Node=S01,Sec=1
Expected result:
Hash3:
$VAR1 = {
'Network=Europe,Network=Ireland,Node=S01,SecAnt=1' => [
'Network=Europe,Network=Ireland,Node=S01,Sec=1,Car=1',
'Network=Europe,Network=Ireland,Node=S01,Sec=1,Car=2',
]
};
sec_ant_1:Network=Europe,Network=Ireland,Node=S01,SecAnt=1
car_1:Network=Europe,Network=Ireland,Node=S01,Sec=1,Car=1###Network=Europe,Network=Ireland,Node=S01,Sec=1,Car=2
Since it should look for a Node value without Car data, and pick the respective SecAnt data. And Based on the Node value the corresponding Sec+Car data(from hash2) needed to be picked by the use of resBy's Node data. How can I do that?