Reputation: 49
Is there a way to combine both keys and values of a hash in one HOA? Let's say i've a sample input like
#NewName OldName
Axc.Sx2.1_Axc.Wx2.1 1BDER
Axc.Sx2.1_Axc.Wx2.1 1ADER
In the above code values of the hash are different but their keys are same whereas in the below code values are same but keys are different.
Axc.Sx2.1_Axc.Wx2.1 1BDER
Axc.Sx2.1_Axc.Wx2.1 1BDER
Axc.Sx2.1 1BDER
Following code can handle the merging of values, but can't handle the keys merging.
while (<$mapF>) {
chomp $_;
next if /^\s*(#.*)?$/;
next if /^\s+.*$/;
##latestRuleName OldRuleName
if ( $_ =~ /(\S+)\s+(\S+)/gi ) {
# create list and append $2
push @{ $mapHash{$1} }, $2;
}
}
Please advise.
Regards, Divesh
Upvotes: 0
Views: 65
Reputation: 35198
If you want a two-way relationship, then you simply need two hashes:
use strict;
use warnings;
my %new2old;
my %old2new;
while (<DATA>) {
my ( $new, $old ) = split ' ';
push @{ $new2old{$new} }, $old;
push @{ $old2new{$old} }, $new;
}
use Data::Dump;
dd \%new2old;
dd \%old2new;
__DATA__
Axc.Sx2.1_Axc.Wx2.1 1BDER
Axc.Sx2.1_Axc.Wx2.1 1ADER
Axc.Sx2.1 1BDER
Outputs:
{
"Axc.Sx2.1" => ["1BDER"],
"Axc.Sx2.1_Axc.Wx2.1" => ["1BDER", "1ADER"],
}
{
"1ADER" => ["Axc.Sx2.1_Axc.Wx2.1"],
"1BDER" => ["Axc.Sx2.1_Axc.Wx2.1", "Axc.Sx2.1"],
}
Upvotes: 1