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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through Multi Dimension hashs

Status
Not open for further replies.

jgiri

Programmer
May 9, 2010
6
US
Hi guys,

I am creating a hash to store my servers. Here is my hash example:
push(@ { $array{examplesite}{'App-servers'}{'Application Server'}{'External IP'} },'test');
push(@ { $array{examplesite}{'App-servers'}{'Application Server'}{'External IP'} },'test1');
push(@ { $array{examplesite}{'App-servers'}{'Database server'}{'External IP'} },'test1');

I am able to get external ip printed by doing this:
for $item ( @ { $array{examplesite}{'App-servers'}{'Application Server'}{'External IP'} }){

print $item;

}

But I have to specify 'Application Server' within my for query, I would like to print everything after {'App-servers'} so I can see database servers as well as app-servers. Any suggestions how I can do that ?
 
Like this?

Code:
[olive][b]foreach[/b][/olive] [blue]$key1[/blue] [red]([/red] [url=http://perldoc.perl.org/functions/keys.html][black][b]keys[/b][/black][/url] [blue]%[/blue][red]{[/red] [blue]$array[/blue][red]{[/red][purple]examplesite[/purple][red]}[/red][red]{[/red][red]'[/red][purple]App-servers[/purple][red]'[/red][red]}[/red] [red]}[/red] [red])[/red] [red]{[/red]
        [olive][b]foreach[/b][/olive] [blue]$key2[/blue] [red]([/red] [black][b]keys[/b][/black] [blue]%[/blue][red]{[/red] [blue]$array[/blue][red]{[/red][purple]examplesite[/purple][red]}[/red][red]{[/red][red]'[/red][purple]App-servers[/purple][red]'[/red][red]}[/red][red]{[/red][blue]$key1[/blue][red]}[/red] [red]}[/red] [red])[/red] [red]{[/red]
                [olive][b]for[/b][/olive] [blue]$item[/blue] [red]([/red]  [blue]@[/blue][red]{[/red] [blue]$array[/blue][red]{[/red][purple]examplesite[/purple][red]}[/red][red]{[/red][red]'[/red][purple]App-servers[/purple][red]'[/red][red]}[/red][red]{[/red][blue]$key1[/blue][red]}[/red][red]{[/red][blue]$key2[/blue][red]}[/red] [red]}[/red][red])[/red][red]{[/red]
                        [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$item[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
                [red]}[/red]
        [red]}[/red]
[red]}[/red]

Annihilannic.
 
Actually one more question, what If I also wanted to loop through examplesite. I tried following code but it's not working, I would also like to print the name of the site along with application server and Ip address


push(@ { $array{examplesite}{'Application Server'}{'External IP'} },'test');
push(@ { $array{examplesite}{'Application Server'}{'External IP'} },'test1');
push(@ { $array{examplesite}{'Database server'}{'External IP'} },'10.0.0.1');

foreach $key1 ( keys %{ $array } ) {

foreach $key2 ( keys %{ $array{$key1} } ) {
foreach $key3 ( keys %{ $array{$key1}{$key2} } ) {
for $item ( @{ $array{$key1}{$key2}{key3} }){
print $item->{ $array{$key1}{$key2}{$key3}};
print "$item\n";
}
}
}

}
 
Use foreach $key1 (keys %array) since you are referring directly to the hash, not indirectly. Also, "array" isn't a very good name for a hash!

You have key3 where it should be $key3.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top