|Problems? Is your data what you think it is?|
Logic trouble parsing a formatted text file into hashes of hashes (of hashes, etc.)by idnopheq (Chaplain)
|on Oct 16, 2004 at 19:11 UTC||Need Help??|
idnopheq has asked for the
wisdom of the Perl Monks concerning the following question:
It's been a while. Ellated to be back!
I have files (Juniper/NetScreen's NSM *.nml files, similar to CheckPoint management station *.W or object*.C files) I need parsed to retrieve specific information.
I'm looking to create hashes of hashes, or at least I think that is the way to go. I need a nudge (and please just a nudge as I'm trying to get my perl legs under me again) toward how to slurp such a file into something useful.
The tabs/parentheses are messing me up big time. Anther hurdle is creating arbitrary hashes (of hashes etc.) by a variable and properly handling relative depth. Lastly, this has to be maintainable (by someone with equal or lesser perl skills to my atrophied ones).
Basically, I don't think I'm approaching my source data properly.
I'd post what I have now (reading the whole file into one big ordered hash and positional searching a la Recipe 6.14) but its broken beyond repair, at best failed to execute with taint checking and -w and 'use strict;' enabled, and exhibits shamefull coding practices beyond that.
UPDATE 3: Reordered and added a timely readmore tag as I'm on the Gates now.
Here is a small piece of the data (with leading 8 character tabs, as indentation is perhaps crutial):
:rip ( :bind_interface (false) :enable (false) :poison_split_horizon (enable :enable ( :poison (false) ) :disable (null) ) :metric (1) :passive_mode (false) :authentication (no-authentication :multiple-md5 ( :md5-key-values () ) ) )
Here is another snipit of the source file, by request. Please see idnopheq's scratchpad for a full source file.
:members () :global-pro ( :report-manager ( :primary ("&0.server.1") :alarm-attack (true) :alarm-other (true) :alarm-traffic (true) :alarm-di (true) :attack-stat (true) :ethernet-stat (true) :flow-stat (true) :log-config (true) :log-info (true) :log-self (true) :log-traffic (true) :policy-stat (true) :proto-dist (true) :server-port (7800) :user-service () ) )For those who might wish to bring up the fw1rules tool, I've tried "liberating" portions thereof to achieve this end. However, it seems these types of files scream out for hashes of hashes (of hashes, etc.). Traversing every line and manually keeping track of tab depth seems a waste of resources (although I'm getting desperate enough to consider it again).
For those REALLY interested in the beast I need to tame, PM me and I will send you the full source data file.
UPDATE 0: a source data file is in idnopheq's scratchpad.
UPDATE 1: I will turn the solution into a module for general consumption once I'm able to get my mind around this. Maybe it'll even be flexible enough for CheckPoint and Juniper/NetScreen files!