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

I have a string 'dd/mm/yy' shoved into @date that is using / as a delimiter I want to say;

@date=split(///,@date); print "Day = $date[0]\n"; print "Month = $date[1]\n"; print "Year = $date[2]\n";
but you can imagine what it thinks of /// :)
can anyone offer the way to make it "Comply" ?

Thankyou.

Replies are listed 'Best First'.
Re: split trying to look for /
by broquaint (Abbot) on Dec 16, 2003 at 11:11 UTC
    You could escape the back-slash, or more readably, pass it in using an alternate delimiter, or simpler still, as a string e.g
    my @date = split '/', '16/12/2003'; print "Day = $date[0]\n"; print "Month = $date[1]\n"; print "Year = $date[2]\n"; __output__ Day = 16 Month = 12 Year = 2003
    And you'll also want to be using a scalar as a second argument to split, not an array.
    HTH

    _________
    broquaint

      You could escape the back-slash, or more readably, pass it in using an alternate delimiter, or simpler still, as a string

      Actually, that's not a string; it's a regular expression with single quote delimiters. The first argument to split() is always a regular expression except in the one special case of ' '. Try splitting on '*' sometime. :-)

        The first argument to split() is always a regular expression

        Has this always been true? I remember a time long ago when I tried to use split "\n", "foo\nbar"; and got an error stating that using double quotes to split on a newline was wrong and that split /\n/, "foo\nbar"; should be used. I just tried a one-liner with warnings and strict enabled and did not get that same warning/error. Would you know why using double quotes used to give funny results?

Re: split trying to look for /
by Taulmarill (Deacon) on Dec 16, 2003 at 11:29 UTC
    you could also use split( /\//, '16/12/2003' ); if you whant to go for regex.
    note, that evriting in the "//" is regex for split and regex has for most special caracters special uses. to avoud that you could use /\Qsome-nasty-chars\E/.
    oh, yes, if you dont know what regex is, look here and then here.
      If you use the m// operator, things get a little more readable, as you can use other delimiters than /: split(m!/!, '16/12/2003").

      Arjen

Re: split trying to look for /
by mcogan1966 (Monk) on Dec 16, 2003 at 19:54 UTC
    Another option is to escape your '/' in the code.
    @date = split(/\//, @date);
    It's a classic mistake to forget that certain characters need to be escaped so that Perl will take them literally.