why does Perl choke here?

Discussion in 'Perl Misc' started by Z. M. Wu, Nov 24, 2003.

  1. Z. M. Wu

    Z. M. Wu Guest

    Perl does not like if $other contains special characters
    like * ] in the following

    my @a=split/$other/,$req;


    My current workaround is

    $other=~ s/[\)\(\*]/_/g;
    $req=~ s/[\)\(\*]/_/g;

    before the split.

    Any insight or better solution would be helpful

    Thanks

    Mr Wu Zong Ming
     
    Z. M. Wu, Nov 24, 2003
    #1
    1. Advertising

  2. Z. M. Wu wrote:
    > Perl does not like if $other contains special characters
    > like * ] in the following
    >
    > my @a=split/$other/,$req;


    No, they are special characters in regular expressions, and need to be
    escaped.

    > My current workaround is
    >
    > $other=~ s/[\)\(\*]/_/g;
    > $req=~ s/[\)\(\*]/_/g;
    >
    > before the split.
    >
    > Any insight or better solution would be helpful


    Better solution:

    my @a = split /\Q$other/, $req;

    See "perldoc perlre".

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Nov 24, 2003
    #2
    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. William Payne

    Why doesn't std::cin choke on this?

    William Payne, Jan 5, 2004, in forum: C++
    Replies:
    10
    Views:
    671
    Chris \( Val \)
    Jan 6, 2004
  2. S Kemplay

    Why does this choke?

    S Kemplay, Nov 7, 2003, in forum: Python
    Replies:
    4
    Views:
    387
    Dennis Lee Bieber
    Nov 9, 2003
  3. S Kemplay

    Why does this choke?

    S Kemplay, Nov 7, 2003, in forum: Python
    Replies:
    2
    Views:
    313
    S Kemplay
    Nov 7, 2003
  4. Tim Peters
    Replies:
    0
    Views:
    381
    Tim Peters
    Nov 8, 2003
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,031
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page