Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: lost in my data structure.

by ivancho (Hermit)
on Nov 15, 2005 at 04:14 UTC ( [id://508477]=note: print w/replies, xml ) Need Help??


in reply to lost in my data structure.

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...

Replies are listed 'Best First'.
Re^2: lost in my data structure.
by lepetitalbert (Abbot) on Nov 15, 2005 at 13:54 UTC

    Hello dear Monks,

    first, thank you all, you guys are great.

    - GrandFather : since one long debbugging session (after I added warnings when my script was already finished) I now ALWAYS start my scripts with strict and warnings. But it's never said enough !

    - graff && ivancho : that's because it's only a small part (but the 'deepest') of the data. Beside 'tcp' is 'udp', and then a lot of other infos

    The goal of all this is to store everything in one db file, and then to check this against a previous snapshot

    I thought this way I could loop through the 'snapshot hash' and check for new keys and modified values

    But maybe it's not the best way ?

    Have a wonderful day

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://508477]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2024-04-24 14:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found