For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2007 > Doubt on Pattern Matching









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Doubt on Pattern Matching
Dharshana Eswaran

2007-08-28, 4:39 am

Hi All,

I have a pattern, which reads as shown below:

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 @m = $abc =~ /$statement/g;

My string reads as shown below:

$abc = "typedef struct _MSG_S
{
CALL_ID callId; /**< call id */
VOICE_STATUS status; /**< Call status */
id_t id; /**< The id of process call */
} TAPI_VOICE_NOTIFY_CALL_STATUS_MSG_S;";


The condition $abc =~ /$statement/g; is not satisfied. The array @m is not
being filled.

I am unable to find out the mistake. Is the pattern regex wrong somewhere?

Thanks and Regards,
Dharshana

Chas Owens

2007-08-28, 4:39 am

On 8/28/07, Dharshana Eswaran <dharshana.ve@gmail.com> wrote:
> Hi All,
>
> I have a pattern, which reads as shown below:
>
> 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 @m = $abc =~ /$statement/g;
>
> My string reads as shown below:
>
> $abc = "typedef struct _MSG_S
> {
> CALL_ID callId; /**< call id */
> VOICE_STATUS status; /**< Call status */
> id_t id; /**< The id of process call */
> } TAPI_VOICE_NOTIFY_CALL_STATUS_MSG_S;";
>
>
> The condition $abc =~ /$statement/g; is not satisfied. The array @m is not
> being filled.
>
> I am unable to find out the mistake. Is the pattern regex wrong somewhere?
>
> Thanks and Regards,
> Dharshana
>



That is not the behaviour I am seeing. Please post a full example
that demonstrates your problem. The following code prints out:

callId is of type CALL_ID
status is of type VOICE_STATUS
id is of type id_t

as expected.

#!/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 $abc = "typedef struct _MSG_S
{
CALL_ID callId; /**< call id */
VOICE_STATUS status; /**< Call status */
id_t id; /**< The id of process call */
} TAPI_VOICE_NOTIFY_CALL_STATUS_MSG_S;";

while ($abc =~ /$statement/g) {
my ($type, $name, $size) = ($1, $2, $3);
$type = "array of $type with $size elements" if defined $size;
print "$name is of type $type\n";
}
Dharshana Eswaran

2007-08-28, 4:39 am

When i write the condition
my @m = $fullStruct =~ /$DLstatement/g;
and try printing the array,
print "\nThe array is @m\n";
It prints nothing.... This is the problem...

Thanks and Regards,
Dharshana
On 8/28/07, Chas Owens <chas.owens@gmail.com> wrote:
>
> On 8/28/07, Dharshana Eswaran <dharshana.ve@gmail.com> wrote:
> not
> somewhere?
>
>
> That is not the behaviour I am seeing. Please post a full example
> that demonstrates your problem. The following code prints out:
>
> callId is of type CALL_ID
> status is of type VOICE_STATUS
> id is of type id_t
>
> as expected.
>
> #!/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 $abc = "typedef struct _MSG_S
> {
> CALL_ID callId; /**< call id */
> VOICE_STATUS status; /**< Call status */
> id_t id; /**< The id of process call */
> } TAPI_VOICE_NOTIFY_CALL_STATUS_MSG_S;";
>
> while ($abc =~ /$statement/g) {
> my ($type, $name, $size) = ($1, $2, $3);
> $type = "array of $type with $size elements" if defined $size;
> print "$name is of type $type\n";
> }
>


Rob Dixon

2007-08-28, 4:39 am

Dharshana Eswaran wrote:
> On 8/28/07, Chas Owens <chas.owens@gmail.com> wrote:
>
> When i write the condition
> my @m = $fullStruct =~ /$DLstatement/g;
> and try printing the array,
> print "\nThe array is @m\n";
> It prints nothing.... This is the problem...


If I use the variable names in your original code:

my @m = $abc =~ /$statement/g;
print "\nThe array is @m\n";

then I get

Use of uninitialized value in join or string at E:\Perl\source\x.pl line 30.
Use of uninitialized value in join or string at E:\Perl\source\x.pl line 30.
Use of uninitialized value in join or string at E:\Perl\source\x.pl line 30.

The array is CALL_ID callId VOICE_STATUS status id_t id

which is what I would expect. Every third array element is undefined because
the optional array index is absent.

HTH,

Rob
Chas Owens

2007-08-28, 4:40 am

On 8/28/07, Dharshana Eswaran <dharshana.ve@gmail.com> wrote:
> When i write the condition
> my @m = $fullStruct =~ /$DLstatement/g;
> and try printing the array,
> print "\nThe array is @m\n";
> It prints nothing.... This is the problem...
>
> Thanks and Regards,
> Dharshana

snip[color=darkred]
snip

Again, please post the smallest complete program that exhibits the
problem you are seeing as I cannot reproduce the problem you are
talking about.
Dharshana Eswaran

2007-08-29, 4:00 am

On 8/28/07, Chas Owens <chas.owens@gmail.com> wrote:
>
> On 8/28/07, Dharshana Eswaran <dharshana.ve@gmail.com> wrote:
> snip
> snip
>
> Again, please post the smallest complete program that exhibits the
> problem you are seeing as I cannot reproduce the problem you are
> talking about.





Actually, i was reading the input from a different location, and the input
was not being read properly. I found that out and corrected it. Thanks for
your suggestions. I am sorry that i was unable to produce a full program
regarding the issue.

Thanks and Regards,
Dharshana

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com