in reply to Re: Surprised by split
in thread Surprised by split

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

Replies are listed 'Best First'.
Re^3: Surprised by split
by Anonymous Monk on Aug 11, 2005 at 16:19 UTC
    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","|"," ",":"

Re^3: Surprised by split
by Anonymous Monk on Aug 11, 2005 at 18:16 UTC
    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