http://qs321.pair.com?node_id=91655


in reply to Splitting each word in a string

I know you've already figured this out but, doesn't split use \s+ by default? So...
chomp ($line = <FH>); @vars = split $line;
Get's you the strings to use for whatever evil purposes you might have.

"So... What do all these little arrows mean?"
~unknown

Replies are listed 'Best First'.
Re: Splitting each word in a string
by Abigail (Deacon) on Jun 26, 2001 at 23:22 UTC
    Well, yeah, split uses /\s+/ by default - sort of. However, the first argument, if any, of split is the regular expression. Only if the first argument is missing, it results to the default. In your example, which of course you haven't tested, you give split one argument. One argument split will split $_, using the regular expression given - in your case, the value of $line. Which is not at all what you want.

    I said "sort of", meaning that the default case (that is, no arguments to split) trailing empty fields are removed - just like in awk. Which doesn't happen if you use /\s+/ as first argument. Use ' ' to get the effect of the default behaviour.

    You know, you would have saved a lot of trouble, from both you and me (and people who thought you spoke the truth and used your suggestion) if you had either tried out your suggestion or had consulted the manual page. But I guess a quick typing without thinking is the fast lane to fame.

    -- Abigail

      trailing empty fields are removed - just like in awk. Which doesn't happen if you use /\s+/ as first argument

      No, the default case is different from /\s+/ in that a leading empty field is ignored. The only way to get Perl to not strip trailing empty fields is to pass a 3rd argument to split, usually -1.

      I won't complain about you having wasted my time by getting this wrong. This isn't comp.lang.perl.* and most of us like it that way. If you want to complain about people wasting your time, then comp.lang.perl.* is probably a better forum for you.

      We're all here to have fun and there are plenty of us to answer questions quite correctly as well as incorrectly (and lots of shades of grey in between) and then discuss the results. Many of us end up learning a lot of Perl in the process, but such isn't the only purpose.

      Sure, checking your answers before you post is prudent and polite, but so is not grousing at people when they make a mistake.

      Have the appropriate amount of fun...

              - tye (but my friends call me "Tye")
      Of course you're right about split. My fingers went faster than my brain. I didn't know about the difference between /\s+/ and ' '. In fact Programming Perl seems to indicate that these are equivalent, but checking perldoc I can see that they are not.

      Saved 'a lot' of trouble? Really? Did this take that much time? As for others thinking I was authoritative, I thought my posting was phrased in a questioning way. Sometimes replies are for the edification of the replier as well. Any way, I don't know about you but I wouldn't blindly implement solutions posted by random contributors (especially lowly acolytes ;-) without running a couple of test cases first.

      Well, I'll continue typing as fast as I can and maybe some day I'll get one right.

      Thanks,
      Ira

      "So... What do all these little arrows mean?"
      ~unknown

        Well, I guess I spend about 15 minutes on your post. 15 minutes I could not use answering other questions - either here or on comp.lang.perl.misc. Testing, and then rejecting (but note you need to have decent knowledge to come up with the right test cases...) an offered solution not only takes time, it also decreases the overall percieved usefulness of this site, and the helpfulness of the community.

        A question left unanswered gives only minimal damage. Wrong or incomplete answers are downright harmful.

        As for Programming Perl, it writes:

        If PATTERN is also omitted or is the literal space, " ", the function splits on whitespace, /\s+/, after skipping any leading whitespace.
        To me, that seems to indicate Programming Perl describes exactly the behaviour I mentioned.

        -- Abigail