The more pressing question is whether you need such a deep structure.. Whenever I see something like
} } } } };
I generally think there's something wrong with the data structure.. As you already found out, iterating over the leaves is a nightmare - also, you'd better be very sure that the categories you divided things into (with some generic hash keys) are the only ways you'd be looking for them. Otherwise, any other search turns into an ugly (hardcoded) mixture of qw/grep map keys values/
Instead, you could consider adding some redundancy in exchange for some clarity/flexibiltiy..
For example, you could have an AoH, say, something like
my @ports = ( { id => 0, ip => "ip1", protocol => "tcp", port => 21, service => "ftp", state => "open", }, { id => 1, ... } );
So, this will force you to repeat "ip1" and "ip2" and "tcp" a few times more than you need to - but you will have several degrees of freedom more than the original structure above, you will be able to iterate through all of your ports immediately, to search on any criteria and in general you will have a better idea of how a port is represented inside your structure - ie, it's just the next element of the array, rather than a leaf in some branching tree..
you can then find, say, all open ports:
my @open_ports = grep {$_->{state} eq "open"} @ports;
It's a sort of OO approach, and I think it adds a lot of flexibility here..
Update expand on why a 5-layer hash is not the best thing since sliced bread...

In reply to Re: lost in my data structure. by ivancho
in thread lost in my data structure. by lepetitalbert

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.