Reputation: 61
I'm trying to compare data in hash in order to find differences. My hash:
$VAR1 = {
10.200.40.10 => {
5678903 => {
'status' => 'ACTIVE',
'age' => '20'
}
8234234 => {
'status' => 'NOT ACTIVE',
'age' => '23'
}
}
10.200.40.11 => {
5678903 => {
'status' => 'NOT ACTIVE',
'age' => '20'
}
8234234 => {
'status' => 'NOT ACTIVE',
'age' => '23'
}
}
10.200.40.12 => {
5678903 => {
'status' => 'NOT ACTIVE',
'age' => '21'
}
8234234 => {
'status' => 'ACTIVE',
'age' => '23'
}
}
}
I would like to check if the same user e.g with ID 5678903 has the same values of status and age on each server. Should I divide it for three separated hashes?
Thanks for all suggestions!
Upvotes: 0
Views: 70
Reputation: 40718
Here is an example how you can compare the values for a given user in the sub hashes. Assuming your hash is called $info
:
use List::Util qw(uniq);
my $info = { ... }; # Set up the hash here
my $user = "5678903";
my $status = get_user_value( $info, 'status', $user );
my $age = get_user_value( $info, 'age', $user );
if ( ((uniq @$status) == 1) && ((uniq @$age) == 1) ) {
say "Same status and age";
}
else {
say "Not same status and age";
}
sub get_user_value {
my ( $info, $key, $user ) = @_;
my @values;
for my $server (keys %$info) {
if (exists $info->{$server}{$user}) {
push @values, $info->{$server}{$user}{$key}
if exists $info->{$server}{$user}{$key};
}
}
return \@values;
}
Upvotes: 1