Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

Easy problem for you, but I canno't figure out how to handle that:

$tmp = '11*22*33';
@see = split('*', $tmp);

This fails. How can I split that string (still using split() ) without altering the $tmp data string?

Thanks for the answer.

Replies are listed 'Best First'.
Re: split() problem
by Corion (Patriarch) on Jan 24, 2007 at 08:44 UTC

    split uses a regular expression and not a plain string for splitting (mostly). Your string * gets interpreted as the regular expression /*/, which is nonsensical. You could have shown us how it "fails" for you. I think you want to use split /\*/ instead, which will split on literal asterisks in your data.

      "split uses a regular expression and not a plain string."

      Thanks for that answer.
      /\*/ works, "\*" fails because of delimiters in regex.
      Bad day for me, many thanks for your help :)

        "\*" fails, because "" interpolates \*, which results in a bare *.

        Watch following:

        $ perl -wle 'print "\*"' *

        So you actually should, if (see Corion's reply), use '\*'.

        "\\*" would work.

        The string literal "\\*" results in the string \*.
        When used as a regexp, the string \* matches *.

        Save yourself a lot of headaches and always use /.../ (or m{...}) with split.

A reply falls below the community's threshold of quality. You may see it by logging in.