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

I am trying to split a string where the deliminator is a forward slash / @simpsons=split(///, $_); It obviously doesn't work. How do I quote the / Thanks, Robert

Replies are listed 'Best First'.
RE: / deliminator
by vroom (His Eminence) on Jan 20, 2000 at 03:39 UTC
    you can do @simpsons=split(/\//,$_); The \ will allow you to escape any of the special characters in regexes. For example: . means any character besides \n(newline) basically so if you wanted to see if a filename had a .txt extension you would do this: if($filename=~/\.txt$/){ #searches for .txt the $ matches the end of the string #do something }
Re: / deliminator
by japhy (Canon) on Jan 20, 2000 at 11:10 UTC
    No one happened to mention the fact that split() operates on the $_ variable. Just a helpful tidbit to pass on:
    @parts = split m!/!, $_; # is really the same as @parts = split m!/!;
    Just some idiomatic Perl.
      Yeah, I did mean to say "by default" in there somewhere.
RE: / deliminator
by Anonymous Monk on Jan 20, 2000 at 21:51 UTC
    You can use regular quotes " " to quote a string used by split.
RE: / deliminator
by Anonymous Monk on Jan 20, 2000 at 03:46 UTC
    Try escaping it \/
RE: / deliminator
by Anonymous Monk on Jan 20, 2000 at 06:53 UTC
    You should be able to do split(/\//,$_)... just escape it with another slash
Re: / deliminator
by Crulx (Monk) on Jan 20, 2000 at 07:09 UTC
    Also, It is important to remember that Perl allows for other characters to indicate a pattern match. For example,
    my @simpsons = split(m{/},$_);
    Use this as further proof that Perl has "more than one way to do it."
    Crulx