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"


In reply to Re: parsing a config file by arturo
in thread parsing a config file by Gorio3721

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.