Reputation: 3165
I managed to create a hash data set with a subroutine,
my %check_ip = (
"data1" => $ip1,
"data2" => $ip2,
"data3" => $ip3
);
'data1' => '127.0.0.1',
'data2' => '192.168.0.1',
'data3' => '192.168.1.1'
This is a simple hash. I am looking to put another key behind this, so that this would become a hash of hash, and look like
config1 =>
'data1' => '127.0.0.1',
'data2' => '192.168.0.1',
'data3' => '192.168.1.1',
What is the best way to do this?
Upvotes: 0
Views: 63
Reputation: 21676
#!/usr/bin/perl
use strict;
use warnings;
my $ip1='127.0.0.1';
my $ip2='192.168.0.1';
my $ip3='192.168.1.1';
my %check_ip = (
config1 => { "data1" => $ip1,
"data2" => $ip2,
"data3" => $ip3, },
);
Access like below:
print $check_ip{config1}{data1}; #output 127.0.0.1
Upvotes: 2
Reputation: 242103
To create a nested hash, you need a hash reference.
my %check_ip = (
data1 => $ip1,
data2 => $ip2,
data3 => $ip3,
);
my %config = ( config1 => \%check_ip );
Upvotes: 2
Reputation: 37146
Since a hash key can only have one value, the nested hash needs to be stored as a hash reference, which is what the curly braces {}
are used for:
my %check_ip = (
config1 => { "data1" => $ip1,
"data2" => $ip2,
"data3" => $ip3, },
);
See perldoc perldsc
for more information on Perl data structures.
Upvotes: 1