|Think about Loose Coupling|
parsing comments in newline-delimited files as listsby Amoe (Friar)
|on Dec 27, 2001 at 02:55 UTC||Need Help??|
Amoe has asked for the
wisdom of the Perl Monks concerning the following question:
I'm rewriting some horrible void-context mapping, and have stumbled upon a few...stumbling blocks :) I'm trying to parse a newline delimited config file, and populate an array and a hash with its contents. The file is basically a flat newline-delimited list containing URLs. This makes parsing slightly easier as we can ignore whitespace (URLs are encoded).
The problem is with comments. I want to ignore everything after a comment, and ignore lines that are made blank because of this. (Basically, I want to parse off comments in the same way the Perl parser does, only with the added bonus of being able to ignore whitespace.) The array should be every enabled line in the file (lines not commented out), and the hash should be keyed with every line in the file, ignoring whether or not it's commented out (effectively stripping comment characters from the line), and the values could be anything (I plan to use it as a lookup hash). I came up with this draft:
%alllines gets populated fine, and just as I expected (here I used 1 as the values for the hash). @enabled_lines doesn't work, though; it's always a null list when it's printed. I expect I am missing something simple here, probably to do with my regex, although that one looks okay to my eyes. Aid?
my one true love