Reputation: 3165
I have a hash of hash value I am getting from a subroutine. Pasting the code chunk below.
sub check_interfaces()
my @interfaces = IO::Interface::Simple->interfaces;
my ($ip, $subnet, $dscp) = @_;
my %check_config = (
"ip" => $ip,
"subnet" => $subnet,
"dscp" => $dscp
);
foreach my $if (@interfaces) {
if (not defined $if->address) {
next;
}
%check_configs = ( config => \%check_config );
my %interface = ($if, $if->address);
if ($check_configs{config}{ip} && $check_configs{config}{ip} =~ /$interface{$if}/) {
print "$check_configs{config}{ip} already configured\n\n";
delete $check_configs{config}{ip};
delete $check_configs{config}{subnet};
delete $check_configs{config}{dscp};
delete $check_configs{config};
}
}
print Dumper %check_configs;
This is giving me output as
root@server:/l3dsr# perl test.pl start
10.75.21.23 already configured
$VAR1 = 'config';
$VAR2 = {};
$VAR1 = 'config';
$VAR2 = {
'dscp' => '2',
'ip' => '10.75.130.24',
'subnet' => '255.255.255.255'
};
I was trying to delete the primary hash key, but it was not getting deleted. If I am not deleting, the output would be
10.75.21.23 already configured
$VAR1 = 'config';
$VAR2 = {
'dscp' => '2',
'ip' => '10.75.21.23',
'subnet' => '255.255.255.255'
};
$VAR1 = 'config';
$VAR2 = {
'dscp' => '2',
'ip' => '10.75.130.24',
'subnet' => '255.255.255.255'
};
What I am trying to do is to get rid of this chunk of hash of hash
$VAR1 = 'config';
$VAR2 = {
'dscp' => '2',
'ip' => '10.75.21.23',
'subnet' => '255.255.255.255'
};
by doing
delete $check_configs{config}{ip};
delete $check_configs{config}{subnet};
delete $check_configs{config}{dscp};
delete $check_configs{config};
But it is not working as expected. What am I doing wrong here?
Added Dumper just after the delete, but there is nothing being printed. However, I am printing the keys, and you can see the duplicate occurrence of the key here.
if ($check_configs{config}{ip} && $check_configs{config}{ip} =~ /$interface{$if}/) {
print "$check_configs{config}{ip} already configured\n\n";
delete $check_configs{config}{ip};
delete $check_configs{config}{subnet};
delete $check_configs{config}{dscp};
delete $check_configs{config};
print Dumper %check_configs;
}
}
print keys %check_configs;
Output::
root@server:/l3dsr# perl test.pl start
10.75.21.23 already configured
configconfigroot@server:/l3dsr#
Borodin: With
$Data::Dumper::Useqq = 1;
print Dumper [ keys %check_configs ];
Output::
root@cdn-fe13:/l3dsr# perl test.pl start
10.75.21.23 already configured
$VAR1 = [
"config"
];
$VAR1 = {
"config" => {}
};
$VAR1 = [
"config"
];
$VAR1 = {
"config" => {
"dscp" => 2,
"ip" => "10.75.130.24",
"subnet" => "255.255.255.255"
}
};
Update::
If I add a delete after the delete $check_configs like
delete $check_configs{config}{subnet};
delete $check_configs{config}{dscp};
delete $check_configs{config};
}
}
if (!keys $check_configs{config}) {
delete $check_configs{config};
}
print keys %check_configs ;
it deletes the empty config keys. However, if I add it above, as
if (!keys $check_configs{config}) {
delete $check_configs{config};
}
if ($check_configs{config}{ip} && $check_configs{config}{ip} =~ /$interface{$if}/) {....
it is not working. I am not sure where it is getting the empty keys from.
Upvotes: 1
Views: 256
Reputation: 3165
Worked with the following code.
delete $check_configs{config}{subnet};
delete $check_configs{config}{dscp};
delete $check_configs{config};
}
}
if (!keys $check_configs{config}) {
delete $check_configs{config};
}
print keys %check_configs ;
Upvotes: 2