Perl match problem

Discussion in 'Perl Misc' started by yezi, Aug 30, 2005.

  1. yezi

    yezi Guest

    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
    yezi, Aug 30, 2005
    #1
    1. Advertising

  2. 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
    Jürgen Exner, Aug 30, 2005
    #2
    1. Advertising

  3. yezi

    John Bokma Guest

    John Bokma, Aug 31, 2005
    #3
  4. 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++)
    > 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\]/)


    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Aug 31, 2005
    #4
  5. John Bokma wrote:
    > "Jürgen Exner" <> 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})/


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

    jue
    Jürgen Exner, Aug 31, 2005
    #5
  6. yezi

    yezi Guest

    Thanks for all the posts , It is really helpful. I am new one in perl.
    thanks
    yezi, Aug 31, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    631
  2. Victor
    Replies:
    2
    Views:
    629
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    356
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    464
    Ian Kelly
    Dec 20, 2011
  5. Volkan Civelek

    Match doesn't match

    Volkan Civelek, Jul 19, 2006, in forum: Ruby
    Replies:
    4
    Views:
    155
Loading...

Share This Page