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

this is really trivial. try it and then try it with the possible substitutions, which by my reading of split should all be the same (If EXPR is omitted, splits the $_ string. If PATTERN is also omitted, splits on whitespace).

Update: keyword also whoops! (hence i should never have thought "split $_"
#!/usr/bin/perl use warnings; use strict; while (<DATA>) { my @line = split / /; #nb. "split $_" returns many uninti +alized values my $bit = shift @line; # "split" returns the a variatio +n of that, print "|$bit| @line"; # but with no newlines } # "split / /,$_" predictably ret +urns the same # as "split / /" __DATA__ holli: s/drowning/draining/ moritz steps out of the way, and watches a nice hole in the wall moritz: time for a counter attack... moritz takes his +5 Samurai Sword of Perl Monk Decapitation... moritz: ... and throws a 1. Damned, I missded holli writes a Perl 6 module to automate aiming moritz: s/missded/missed/ holli laughs at [moritz], then slips over a globular dice moritz throws a discontinued perl 6 compiler at [holli] holli smiles as he sees hist watcher elemental tearing the compiler ap +art moritz: why do I have to lose all the time? holli: <c>try{ harder(); }</c> ;-)
Thanks Kyle for noticing my stupidity

Replies are listed 'Best First'.
Re: about split
by kyle (Abbot) on Mar 12, 2008 at 15:42 UTC

    I think the confusion here is that "split" is not the same as "split / /". Rather, "split" is the same as "split /\s+/".

    Also, if you "split $_", Perl thinks you're using $_ as your pattern. You can't leave out the pattern and still pass a string to split. If you specify a string, you also have to specify a pattern.

      Rather, "split" is the same as "split /\s+/"

      Actually, it's not. The only way to avail yourself of the magical default split whilst processing other than $_, is to use ' ' or " " in place of the regex. See the first and last examples below:

      C:\test>p1 $_ = ' the quick brown fox ';; print join'|', split;; ## default split no args (leading null field +omitted) the|quick|brown|fox print join'|', split /\s+/;; ## \s+ includes leading null field |the|quick|brown|fox print join'|', split / /;; ## same as split /\s/ |||the|quick||||brown|fox print join'|', split ' ';; ## Same as no arg split the|quick|brown|fox

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      "split" is the same as "split /\s+/".

      Not quite the same:

      no warnings 'uninitialized'; $_ = ' a string with leading whitespace'; print join( ':', split ), "\n"; print join( ':', split /\s+/ ), "\n"; ______ a:string:with:leading:whitespace :a:string:with:leading:whitespace
A reply falls below the community's threshold of quality. You may see it by logging in.