I agree with the other responses, your way looks fine. Sometimes it is worth considering "self documenting data" which can help document your code.
For example
next unless $item->{is_a_switch};
gives you a good indication of what the question is and what sort of answer you can expect.
As has been pointed out, with this approach you are more than halfway to having an object.
#!/usr/local/bin/perl
use strict;
use warnings;
my %network = get_network();
for my $blade (keys %network){
for my $item (@{$network{$blade}}){
next unless $item->{is_a_switch};
print qq{$item->{item_name}\n};
for my $kit (@{$item->{more_kit}}){
print qq{ kit: $kit->{kit_name}\n};
}
}
}
sub get_network {
my %network = (
01/01 => [
{
is_a_switch => 0,
item_name => q{router},
more_kit => 0,
},
{
is_a_switch => 1,
item_name => q{hpswitch1},
more_kit => [
{
port => q{port1},
kit_name => q{workstation1},
},
{
port => q{port2},
kit_name => q{workstation2},
},
],
}
],
01/02 => [
{
is_a_switch => 1,
item_name => q{hpswitch2},
more_kit => [
{
port => q{port1},
kit_name => q{server4},
},
{
port => q{port2},
kit_name => q{server5},
},
],
}
],
01/03 => [
{
is_a_switch => 0,
item_name => q{server2},
more_kit => 0,
},
],
);
return %network;
}
output
hpswitch1
kit: workstation1
kit: workstation2
hpswitch2
kit: server4
kit: server5
|