For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > August 2005 > Perl match problem









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 Perl match problem
yezi

2005-08-30, 6:58 pm

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ürgen Exner

2005-08-30, 6:58 pm

yezi wrote:
> 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


John Bokma

2005-08-31, 3:58 am

"Jürgen Exner" <jurgenex@hotmail.com> wrote:

> (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})/


--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html

Josef Moellers

2005-08-31, 3:58 am

yezi wrote:
> 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=3D-25;$j<=3D25;$j++)
> if($parts[0] =3D~ /RD[$j]/)
>=20
> I am not sure the is expression, since the code can not work. Thanks
> for any=20
> comments


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

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


--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

Jürgen Exner

2005-08-31, 7:56 am

John Bokma wrote:
> "Jürgen Exner" <jurgenex@hotmail.com> wrote:
>
>
> 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



yezi

2005-08-31, 6:57 pm

Thanks for all the posts , It is really helpful. I am new one in perl.
thanks

Sponsored Links







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

Copyright 2008 codecomments.com