artist has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Capture Spaces
by trs80 (Priest) on Feb 07, 2002 at 21:15 UTC
    From the Parse::RecDescent perldocs'
    # Change the universal token prefix pattern # (the default is: '\s*'): $Parse::RecDescent::skip = '[ \t]+';
    So in this case adding something like:
    $Parse::RecDescent::skip = '';
    Will produce the results you wanted based on the sample data.
Re: Capture Spaces
by runrig (Abbot) on Feb 08, 2002 at 00:42 UTC
    You realize that Parse::RecDescent or even a regex is way overkill for this problem, and you'd be better off with substr or unpack. If you're just learning or playing with Parse::RecDescent, then ok, but for real code I wouldn't use it here.
Re: Capture Spaces
by merlyn (Sage) on Feb 07, 2002 at 21:04 UTC
    Why don't you wait 15-20 minutes for an answer to this VERY SAME POST in comp.lang.perl.misc (which I already answered) rather than POST IT SOMEWHERE ELSE without noting that it's cross posted.

    {sheesh}

    -- Randal L. Schwartz, Perl hacker

      Hi Randal,
      Your answer rocks..
      I had not seen my post after 4 hours, So I posted on perlmonks.org.
      Take care,

      Aritst