Perl match problem

Y

yezi

If I want to find the text wheather match the RD[$j] $j is the any
number
between -25 to 25, can I write the following sentence :
for ($j=-25;$j<=25;$j++)
if($parts[0] =~ /RD[$j]/)

I am not sure the is expression, since the code can not work. Thanks
for any
comments
 
J

Jürgen Exner

yezi said:
If I want to find the text wheather match the RD[$j] $j is the any
number
between -25 to 25, can I write the following sentence :
for ($j=-25;$j<=25;$j++)

This is better written as
for (-25..25) {
if($parts[0] =~ /RD[$j]/)


Yuk! Why the loop?
(untested, just to give you an idea):

if (/RD(\d*)/) and $1 >= -25 and $1 <= 25) ....

jue
 
J

John Bokma

Jürgen Exner said:
(untested, just to give you an idea):

if (/RD(\d*)/) and $1 >= -25 and $1 <= 25) ....

Uhm, since you expect at least one digit, at most 2, and there could be a
minus sign:

/RD(-?\d\d?)/

or

/RD(-?\d{1,2})/
 
J

Josef Moellers

yezi said:
If I want to find the text wheather match the RD[$j] $j is the any
number
between -25 to 25, can I write the following sentence :
for ($j=-25;$j<=25;$j++)
if($parts[0] =~ /RD[$j]/)

I am not sure the is expression, since the code can not work. Thanks
for any
comments

You do know that [ and ] are meta characters and must be escaped when
matched?

if ($parts[0] =~ /RD\[$j\]/)
 
J

Jürgen Exner

John said:
Uhm, since you expect at least one digit, at most 2, and there could
be a minus sign:

/RD(-?\d\d?)/

or

/RD(-?\d{1,2})/

Thanks. I also forgot the enclosing square brackets for the number, BTW.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top