areyouready344
asked on
How to parse multiline data records using Perl regular expression?
This line (warn "could not parse block:\n$_\n";) of the attached code gives a warning when the data record contains these lines: Which tells me if there is no line between @ line and window line, the code will not parse.
@dldldldld
window
dfjldfldkldsjfl
difkjdljfdlsfld
fdlsjfdlskfdlsfd
But passes when in this line format
@dldldldld
dfdfsfdsfdsf
window
dfjldfldkldsjfl
difkjdljfdlsfld
fdlsjfdlskfdlsfd
I think the solution is to change this line of code meet this new condition but don't know how.
unless (m{^([^\n]+)\n.*?\nwindow[ ^\n]*\n(.* )$}ms)
@dldldldld
window
dfjldfldkldsjfl
difkjdljfdlsfld
fdlsjfdlskfdlsfd
But passes when in this line format
@dldldldld
dfdfsfdsfdsf
window
dfjldfldkldsjfl
difkjdljfdlsfld
fdlsjfdlskfdlsfd
I think the solution is to change this line of code meet this new condition but don't know how.
unless (m{^([^\n]+)\n.*?\nwindow[
#!/usr/bin/perl
use strict;
use warnings;
$/='@';
open 'FH','</home/bwalker/final/real_data_test.txt' or die "Unable to open file: $!";
print "<table border=\"2\">\n";
while (<FH>) {
s{\s*\@$}{}ms;
my $file_cnt = scalar split /\n/;
unless (m{^([^\n]+)\n.*?\nwindow[^\n]*\n(.*)$}ms) {
warn "could not parse block:\n$_\n";
next;
}
my ($test, @lines) = ($1, split /\n+/, $2);
my $sub_cnt = 1 + scalar @lines;
print '<tr align="right" ><td width="50" align="left" rowspan="',scalar(@lines),"\">$test</td><td width=\"400\" align=\"left\">",shift(@lines),"</td></tr>\n",map({
"<tr align=\"right\" ><td width=\"400\" align=\"left\">$_</td></tr>\n" } @lines);
}
print "</table>\n";
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER