http://qs1969.pair.com?node_id=259836


in reply to parsing a config file

The following is untested; it's also not uber-leet, but then what do I care about that? =)

# place to put everything my @queue_managers; # a hash to store the key/value pairs for each queue manager my %conf = (); while (<DATA>) { # see if we have a new "queuemanager" line and if there's any data +to save if ( /^QueueManager/ && %conf ) { # save what we have already stored push @queue_managers, { %conf }; # clear the hash for the next one %conf=(); next; } # now check key/value pair lines and if we've got one, put its # data into the hash. I'm assuming that values are bracketed by # spaces, so I just grab the longest set of non-spaces if ( my ($key, $value ) = /^\s*(\w+)\s*=\s*(\S+)/ ) { $conf{$key} = $value; } } # ahh, the last entry probably hasn't been saved push @queue_managers, {%conf} if %conf;

What this does is put each configuration entry, stored as a hash reference, into an array. If you just want to print the darn thing out, you could print out the hash instead of push ing an anonymous copy (which is what the { %conf } syntax does) onto the array.

What this code gives you is an array, each of whose members corresponds to one of the "QueueManager" entries (where that means, "follows a QueueManager line and ends at a new QueueManager line or the end of the file, whichever comes first).

Managing to do something with this data structure, or changing it to a diffferent kind of structure (it strikes me that what you'd really like if you're using this as a config file is a hash of hashes, where the key of each hash is the name of the queue manager, and whose values are the other values.) is something I'll let you figure out for yourself, with a tip of the hat to References quick reference, perlref, perllol, and perldsc.

HTH and good luck!

If not P, what? Q maybe?
"Sidney Morgenbesser"

Replies are listed 'Best First'.
Re: Re: parsing a config file
by Gorio3721 (Acolyte) on May 23, 2003 at 20:57 UTC
    This mostly works but it also picks up all the <key> = <value> pairs before the first queuemanager: stanza. I did actually use a hash of hases in my working solution. I just wanted to satisfy my curitosity by getting this to work. Thanks, Gorio