Hi
#!/usr/local/bin/perl -w
use Parse::RecDescent;
$::RD_ERRORS = 1; # Make sure the parser dies when it encounters an er
+ror
$::RD_WARN = 1; # Enable warnings. This will warn on unused rules &c
+.
$::RD_HINT = 1; # Give out hints to help fix problems.
my $grammer = q{
record: id name
id: m[(.{6})] { print "id => $1 " if $1 }
name: m[(.*)] { print "name => $1 "; }
};
my $parser = Parse::RecDescent->new($grammer);
while(<DATA>){
chomp;
print "Matches: ", $parser->record($_),"\n";
}
__DATA__
1A34 John
3B5 Mary
Bill
1+3 Matt
Wilma
2x3 Gary
id => 1A34 name => John Matches: 1
id => 3B5 name => Mary Matches: 1
Use of uninitialized value in print at x2.pl line 16, <DATA> line 3.
Matches:
id => 1+3 name => Matt Matches: 1
Use of uninitialized value in print at x2.pl line 16, <DATA> line 5.
Matches:
id => 2x3 name => Gary Matches: 1
Hi Monks
In above data, Bill and Wilma are not assigned any ID.
But code doesn't ouput that way. I would like to have output as
id => name => Bill
I would like to capture the spaces in front of Bill and Wilma as ID.
Thanks,
Artist
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.