in reply to (Ovid) Re: ignoring empty returns from split
in thread ignoring empty returns from split

Ok, I tried this, but for some reason it still didn't work. I have code as follows:
my @line = grep { length > 0 } split ( /\S+/, $_ ); print "$line[0]\t$line[1]\n";
Where $_ will look something like this:
STATUS mandatory
Now, I'd assume that it would therefore print:
STATUS mandatory
But instead, all I get are blank lines. Can anyone think what's going wrong?

Also, grep{length>0} is much preferable to grep{length}. The latter accomplishes absolutely nothing, fails for returns of "" so is actually somewhat buggy, and detracts from readability. The only real reason I can see to use it is a desire to show off, an impulse which should be squelched at every opportunity.

UPDATE:Ok, I'm an idiot, but I will leave my stupidity on display for others benefit. I just noticed that I'm splitting on \S+, not \s+. So amazingly, perl did exactly what I told it to do. I just told it to do something extremely counter-productive : )

"If you have any trouble sounding condescending, find a Unix user to show you how it's done."
- Scott Adams

Replies are listed 'Best First'.
Re: Re: (Ovid) Re: ignoring empty returns from split
by busunsl (Vicar) on Apr 24, 2001 at 19:47 UTC
    You're splitting by \S+, this is a capital S and means anything but whitespace.
    This is probably not what you want, use a lowercase s.