Can someone help? I have given myself the challenge of doing some simple parsing, but in a complex way. Without focusing on why I choose to do this, can someone guide me towards a viable solution?
Given the following input:
name1=value1
name2 = value2
This code parses it:
while (<$input>)
{
chomp;
next if /^ \s* #/;
next if /^ \s* $/;
if (/^ \s* ([^=\s]+) \s* = \s* (.+) $/x)
{
# name is in $1, value is in $2
}
}
That's not the question though. The question is, how would I parse the following:
name1=value1
name2 = value2
name3 = value3
but wait, there is
more
name4=
value4
With Perl that has the form:
my $contents = do {local $/; <$input>};
while ($contents =~ / ANSWER_HERE /msg)
{
# name is in $1, value is in $2
}
Specifically, I want to use the //g form, to iterate over the string, and not perform a line-by-line parse, as in the first example. My attempts have thus far failed. The closest I got (without success) was:
my $contents = do {local $/; <$input>};
my $name = qr/\s* [^=\s]+ \s*/x;
while ($contents =~ /^ ($name) = \s* (.+) (?= ^ $name = | $ ) /msg
+x)
{
# name is in $1, value is in $2
}
pbeckingham - typist, perishable vertebrate.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.