Thank you. But i am unable to understand the working of the code which you
have written. Can you please explain it?
Thanks and Regards,
Dharshana
On 6/28/07, Chas Owens <[EMAIL PROTECTED]> wrote:
On 6/27/07, Dharshana Eswaran <[EMAIL PROTECTED]> wrote:
> On 6/28/07, Tom Phoenix <[EMAIL PROTECTED]> wrote:
> >
> > On 6/27/07, Dharshana Eswaran <[EMAIL PROTECTED]> wrote:
> >
> > > I am unable to get a generalised way in which it can extract them as
few
> > > structures have comments, few does not hav comments etc.
> >
> > Does the data have some defined grammar, or a definable one at least?
>
>
>
> The defined Grammer here is
> {
> xyz1 abc1; /*Comments*/
> xyz2 abc2;
> xyz3 abc3[req];
> xyz4 abc4[req]; /*Comments*/
> };
>
> Here, i have defined different possibility of occurences of the
structure
> elements. If i could get a regex for extracting xyz1, xyz2, xyz3, xyz4
and
> abc1, abc2, abc3[req], abc4[req] would be helpful. Here, the comments
are of
> no use, i just need to ignore them.
>
> >If you are up to using Parse::RecDescent, it will probably do the job.
>
> I am restricted from using modules and i am unable to come up with a
regex
> or regexes to do this job.
>
> >http://search.cpan.org/author/DCONWAY/Parse-RecDescent-1.94
> >/lib/Parse/RecDescent.pod
>
> >Hope this helps!
>
> >--Tom Phoenix
> >Stonehenge Perl Training
>
> Can anyone guide me in this?
>
> Thanks and Regards,
> Dharshana
>
It is fragile, but here are a set of regexes that parse the string you
mentioned. I did notice that this string differs significantly from
the ones you gave earlier and this set of regexes will not correctly
handle them.
#!/usr/bin/perl
use strict;
use warnings;
my $comment = qr{\s* (?:/\* .*? \*/ \s*)*}xs;
my $identifier = qr{ [A-Za-z_]\w* }xs;
my $statement = qr{
\s*
($identifier)
\s+
($identifier)
\s*
(?: \[ (.*?) \] )?
\s*
;
\s*
$comment?
}xs;
my $str = <<EOS;
{
xyz1 abc1; /*Comments*/
xyz2 abc2;
xyz3 abc3[req];
xyz4 abc4[req]; /*Comments*/
};
EOS
my @m = $str =~ /$statement/g;
my $iter = by_n(3, [EMAIL PROTECTED]);
while ((my ($type, $var, $elems) = $iter->()) == 3) {
if ($elems) {
$type = "array of $type with $elems elements";
}
print "type is $type and variable is $var\n";
}
sub by_n {
my ($n, $a) = @_;
my $i = 0;
sub {
return undef if $i > $#$a;
my @ret = @{$a}[$i .. $i + $n - 1];
$i += $n;
return @ret;
}
}