Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Hash of Hashes 2

Status
Not open for further replies.

netman4u

Technical User
Mar 16, 2005
176
US
Hello all,

I have the following config file I nees to load in a way that I can easily access the commands:

Code:
#
#
#  Vendor	Device		Commands
#
Foundry&	IMG&		show ip nat statistics,sho server sessions, , , , , , , ,
Foundry&	DLB&		sho server sessions, , , , , , , , ,
Cisco&		BSR&		, ,sho idb, , , , , , ,
Cisco&		CAT&		, ,sho idb, , , , , , ,
Cisco&		EGR&		, ,sho idb, , , , , , ,
Cisco&		EXT&		, ,sho idb, , , , , , ,
Cisco&		FER&		, ,sho idb, , , , , , ,
Cisco&		FVR&		, ,sho idb, , , , , , ,
Cisco&		GTA&		, ,sho idb, , , , , , ,
Cisco&		ILB&		, ,sho idb, , , , , , ,
Cisco&		MCR&		, ,sho idb, , , , , , ,
Cisco&		MSS&		, ,sho idb, , , , , , ,
Cisco&		NPA&		, ,sho idb, , , , , , ,
Cisco&		VRR&		, ,sho idb, , , , , , ,
Cisco&		XER&		, ,sho idb, , , , , , ,
Cisco&		SOR&		, ,sho idb, , , , , , ,
Cisco&		VRS&		, ,sho idb, , , , , , ,

I am trying to create a hash of hashes as below:

Code:
foreach(@cfg) {
	next if /^#/; 
	@tmp = split(/&/,$_);
	$tmp[1] =~ s/^\s+//;
        $tmp[1] =~ s/\s+$//;
	%cmd = ('$tmp[0]' => {'$tmp[1]' => '$tmp[2]'});
	my $hoh_cnt = scalar(keys(%cmd));
	@count = split(/,/,$tmp[1]);
}
print $cmd{'Foundry'}->{'IMG'};

However the print statement prints nothing. I am trying to access the commands for the "Vendor" and "Device" from my config file.

Any help is appriciated.

Nick

If at first you don't succeed, don't try skydiving.
 
This might get you started:
Code:
my %HoH;

while (<DATA>) {
	next if /^\s*#/;
	chomp;
	my ($vender, $device, $rest) = split(/\s*\&\s*/, $_);
	if ($vender) {
		$HoH{$vender}->{$device} = $rest;
	}
}

__DATA__
#
#  Vendor    Device        Commands
#
Foundry&    IMG&        show ip nat statistics,sho server sessions, , , , , , , ,
Foundry&    DLB&        sho server sessions, , , , , , , , ,
Cisco&        BSR&        , ,sho idb, , , , , , ,
Cisco&        CAT&        , ,sho idb, , , , , , ,
Cisco&        EGR&        , ,sho idb, , , , , , ,
Cisco&        EXT&        , ,sho idb, , , , , , ,
Cisco&        FER&        , ,sho idb, , , , , , ,
Cisco&        FVR&        , ,sho idb, , , , , , ,
Cisco&        GTA&        , ,sho idb, , , , , , ,
Cisco&        ILB&        , ,sho idb, , , , , , ,
Cisco&        MCR&        , ,sho idb, , , , , , ,
Cisco&        MSS&        , ,sho idb, , , , , , ,
Cisco&        NPA&        , ,sho idb, , , , , , ,
Cisco&        VRR&        , ,sho idb, , , , , , ,
Cisco&        XER&        , ,sho idb, , , , , , ,
Cisco&        SOR&        , ,sho idb, , , , , , ,
Cisco&        VRS&        , ,sho idb, , , , , , ,
 
Thanks rharsh, that did it. I think I am finally starting to get a handle on multi dimensional data structures.

If at first you don't succeed, don't try skydiving.
 
Good, glad to help.

Have you looked through perldoc perldsc and perldoc perllol - those two docs have lots of information on data structures that you might find helpful.
 
I have. It just seems like I have had some mental block in the past that prevented me from understanding the concept. Probably due to the fact that Perl is my first programming language and I only do Perl a month or two out of the year.

Thanks again.

Nick

If at first you don't succeed, don't try skydiving.
 
Nick

The Data::Dumper module is invaluable when debugging multi-dimensional data structures, especially those that are produced by other modules like XML::Simple.
Code:
use Data::Dumper;
print Dumper(%HoH);
Gives a nice, indented, graphic representation (within the limitations of text-only output).

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top