Wes Groleau wrote:
>
> Text is optional and is everything after the tag.
That condition is not reflected in this regex, which requires one or
more whitespace characters after Tag, or else it won't capture the
data as expected:
> /^\s*(\d+)\s+(@\S+@)?\s*(\S+)\s+(.*)/;
---------------------------------^^^
It's better written as:
/^\s*(\d+)\s+(@\S+@)?\s*(\S+)(?:\s+(.+))?/;
---------------------------------^^^-------^^
Other comments:
You should declare your variables and run your script with strictures
and warnings enabled.
use strict;
use warnings;
> Here is what is not working:
That's a pointless description of your problem, isn't it? You'd better
explain what you expect the script to output, what it actually
outputs, and which error and warning messages you receive (if any).
> @lines = <STDIN>; # Read all lines into array @lines
Are you really using the STDIN filehandle for reading the file? Since
STDIN is a special filehandle, you should use some other name.
Basically I think your code can be shortened to:
while (<FILE>) {
chomp;
my ($Level, $ID, $Tag, $Text) =
/^\s*(\d+)\s+(@\S+@)?\s*(\S+)(?:\s+(.+))?/;
$Level ||= 0;
$ID ||= '';
$Text ||= '';
print "$Level\n$ID\n$Tag\n$Text\n";
}
--
Gunnar Hjalmarsson
Email:
http://www.gunnar.cc/cgi-bin/contact.pl