splitting problem

Discussion in 'Perl' started by Jan, Oct 31, 2003.

  1. Jan

    Jan Guest

    Hi!
    I have to split a string with a pattern which contains sometimes chars
    like + \ (the code is needed in an interpreter, written in Perl, of my
    own scripting language, so I never know the pattern exactly).

    split(/$pattern/, $string);

    It works well most of all time, but if the pattern is + the execution
    of the program stops and perl prints an error message.

    $pattern = '+';
    split(/"$pattern"/, $string); #ok

    $pattern = '#';
    split(/"$pattern"/, $string); #doesn't work really, but no
    error
    #occurs

    Can anyone tell me how to solve this problem?
    Jan, Oct 31, 2003
    #1
    1. Advertising

  2. Jan wrote:
    > I have to split a string with a pattern which contains sometimes chars
    > like + \ (the code is needed in an interpreter, written in Perl, of my
    > own scripting language, so I never know the pattern exactly).
    >
    > split(/$pattern/, $string);
    >
    > It works well most of all time, but if the pattern is + the execution
    > of the program stops and perl prints an error message.
    >
    > $pattern = '+';
    > split(/"$pattern"/, $string); #ok
    >
    > $pattern = '#';
    > split(/"$pattern"/, $string); #doesn't work really, but no error
    > #occurs
    >
    > Can anyone tell me how to solve this problem?


    I don't think any of those works, since you are actually trying to
    split on the strings '"+"' and '"#"'.

    You should escape special characters. Try:

    split /\Q$pattern/, $string;

    Also, stop asking questions in this newsgroup. It's defunct. Use
    comp.lang.perl.misc instead.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Oct 31, 2003
    #2
    1. Advertising

  3. Jan

    nikolay Guest

    ÷ ÐÉÓØÍÅ Fri, 31 Oct 2003 00:35:15 -0800, Jan ÎÁÐÉÓÁÌ:

    > Hi!

    Hi

    > split(/$pattern/, $string);
    >
    > It works well most of all time, but if the pattern is + the execution
    > of the program stops and perl prints an error message.

    This is most like '+' means repeat more than zero.
    I recommend do something like that if you want pattern to be a string
    (not regexp):
    $pattern = "string";
    $qpattern = quotemeta($pattern)
    split(/$pattern/,$string);


    --
    With best wishes Nikolay
    mail: -kpi.kiev.ua
    ICQ#: 136497739
    nikolay, Oct 31, 2003
    #3
  4. Jan

    Jan Guest

    Thank you, the examples work!
    .... and excuse me about posting this thread in the wring newsgroup.
    Jan
    Jan, Oct 31, 2003
    #4
    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. Mothra
    Replies:
    1
    Views:
    394
    vkeyboard
    Jan 12, 2005
  2. John Ericson
    Replies:
    0
    Views:
    423
    John Ericson
    Jul 19, 2003
  3. Mark
    Replies:
    0
    Views:
    440
  4. John Dibling
    Replies:
    0
    Views:
    412
    John Dibling
    Jul 19, 2003
  5. Angelo Secchi

    Problem in splitting a string

    Angelo Secchi, Jul 22, 2004, in forum: Python
    Replies:
    10
    Views:
    494
    Byron
    Jul 22, 2004
Loading...

Share This Page