Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Surprised by split

by Anonymous Monk
on Aug 11, 2005 at 14:05 UTC ( [id://482964]=note: print w/replies, xml ) Need Help??


in reply to Surprised by split

As explained by others, this is caused by repeated interpretation of the backslash. Or rather, lack of backslashes so it can be interpreted more than once. That's why I prefer to use [|] (and I think "Perl Best Practises" advices that too), then you can do both:
split "[|]", $str;
and
split /[|]/, $str;
without having to wonder how many backslashes you need.

Anonymous Backslash Killer

Replies are listed 'Best First'.
Re^2: Surprised by split
by sk (Curate) on Aug 11, 2005 at 15:42 UTC
    Sometimes I have to ask the user to specify the delimiter on the file and they might enter a pipe from the command line.

    This is how I was handling them -

    $opt_d = "\\|" if ($opt_d eq "|");

    Since i am just splitting the file based on just a string, I guess the /[$opt_d]/ would do the trick!

    Thanks Anonymous Monk

    -SK

      That would cause a problem if $opt_d equals ^. Perl seems to handle the cases /[-]/, /[[]/, /[]]/ as the appropriate single character class, but it trips on /[^]/. I wonder whether that's a bug (or a yet unimplemented feature).

      Of course, you can get all sorts of unexpected nonsense if $opt_d is longer than a single character. Or if it's the empty string.

        Thanks for the info on pitfalls!!! For me, usually it is from a well behaved/nicely formatted data and almost always contains one of the following delimiters ",","\t","|"," ",":"

      Wouldn't quotemeta avoid this problem, as well as the ones described below?
        You are right! That should do it!!!

        perl -e ' $str = "hi|there"; $d = quotemeta("|"); @a = split /$d/, $st +r; print +($_,$/) for (@a);' __END__ hi there
Re^2: Surprised by split
by Anonymous Monk on Aug 11, 2005 at 15:40 UTC
    Or you can use regular expression quotes when you're giving a regular expression. Using a character class might be helpful if you don't want to learn the language, but I would rather people just learn the language.
      Or you can use regular expression quotes when you're giving a regular expression.
      What do you mean by that? qr? Then you still need to escape the pipe.
      Using a character class might be helpful if you don't want to learn the language, but I would rather people just learn the language.
      What on earth do you mean by that?
        What do you mean by that? qr? Then you still need to escape the pipe.

        That's true. I never indicated anything to the contrary.

        What on earth do you mean by that?

        The suggestion to use a character class makes split happy, even when passed a string instead of a proper regular expression. That makes it easier for people who don't know enough about the language they're using to get things working. This is certainly one way to accomplish the goal. Another way would be for those people to actually learn the language, instead of learning ways to hack around their lack of knowledge. I am simply advocating the latter strategy over the former.

Re^2: Surprised by split
by GrandFather (Saint) on Aug 11, 2005 at 20:39 UTC

    So you are suggesting that I should use a character class trick to avoid quoting a character that I should know needs to be quoted in a context I should know is a regex and that I should type an extra character to achieve this?

    I'm lazy and will take the good advice of others who have suggested that /\|/ is the way to do it.


    Perl is Huffman encoded by design.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://482964]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-19 04:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found