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

Hello, I was trying to test how split works and ran into this issue. Please help:
#test 1 $s ="This is a test"; $d =" "; print "--- output 1 --- \n"; @ar = split($d, $s); foreach $i (@ar) { print "|$i| \n"; } #test 2 #how come this one prints differently? print "--- output 2 --- \n"; @ar = split(" ", $s); foreach $i (@ar) { print "|$i| \n"; }
First one prints this:
--- output 1 --- |this| || || |is| || || |a| || || || |test|
I think this is the correct behavior. But the second one prints like this(when delimiter is used directly):
--- output 2 --- |this| |is| |a| |test|
I am using perl 5.8.5 on linux. Thanks a lot!
I am sorry I made a change to the question -- originally I had $delim = " " (instead of $d = " ") which was a typo.

Replies are listed 'Best First'.
Re: Interesting issues with split function
by dragonchild (Archbishop) on Dec 03, 2004 at 17:31 UTC
    " " is special-cased to be equivalent to /\s+/. It's mentioned in perldoc -f split, in case you're wondering.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Interesting issues with split function
by BrowserUk (Patriarch) on Dec 03, 2004 at 17:31 UTC

    Because you defined $delim = " " but then used @ar = split($d, $s);.

    use strict would have caught that typo for you.


    Examine what is said, not who speaks.
    "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
    "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: Interesting issues with split function
by Joost (Canon) on Dec 03, 2004 at 17:38 UTC
    From the docs:
    As a special case, specifying a PATTERN of space (' ') will split on white space just as "split" with no arguments does. Thus, "split(' ')" can be used to emulate awk's default behavior, whereas "split(/ /)" will give you as many null initial fields as there are leading spaces. A "split" on "/\s+/" is like a "split(' ')" except that any leading whitespace produces a null first field. A "split" with no arguments really does a "split(' ', $_)" internally.

    So $delim is always interpreted as a pattern, even if it contains only a single space, because it is not the " " literal.

Re: Interesting issues with split function
by ikegami (Patriarch) on Dec 03, 2004 at 18:11 UTC

    (It seems your question was edited. It would have been nice if you had left the original question there, and either posted a reply, or appended your update to your original post.)

    If I were to guess why it prints differently, I'd say the special case for split(" ") is handled at compile time and looks for a string literal. Use $d = "\\s+"; or $d = qr/\s+/; instead of $d = " ";.