Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Counting the number of items returned by split without using a named array

by blazar (Canon)
on May 03, 2006 at 10:50 UTC ( #547114=note: print w/replies, xml ) Need Help??


in reply to Re^2: Counting the number of items returned by split without using a named array
in thread Counting the number of items returned by split without using a named array

GAWD! Well, the fact that you write "optimized" yourself suggests that it is really an unwanted side effect of an optimization... may I push it as far as to dare to say that it is a bug?

Well, another trick that I verified not to be flawed is:

my $count=map $_, split;

of course it doesn't just taste as good... hmmm, how 'bout:

my $count=+(split); # ?!?

(also verified!)

$ perl -lpe '$_=+(split)' foo 1 bar baz 2

Replies are listed 'Best First'.
Re^4: Counting the number of items returned by split without using a named array
by salva (Canon) on May 03, 2006 at 11:15 UTC
    may I push it as far as to dare to say that it is a bug?

    well, probably not, as it is well documented on perlfunc

    A workaround is to set the limit explicetly to undef (though, it generates a warning):

    $count = () = split ' ', $_, undef;

    amazingly, setting it to 0 doesn't work:

    $ perl -MO=Deparse -e '$a = () = split(" ", $_, undef)' $a = () = split(" ", $_, undef); $ perl -MO=Deparse -e '$a = () = split(" ", $_, 0)' $a = () = split(" ", $_, 1);

    A better workaround that doesn't generate warnings is to use a zero-but-true value:

    $count = () = split ' ', $_, '0e0';
    that is parsed as:
    $ perl -MO=Deparse -e '$a = () = split(" ", $_, "0e0")' $a = () = split(" ", $_, '0e0');
Re^4: Counting the number of items returned by split without using a named array
by BrowserUk (Pope) on May 03, 2006 at 12:32 UTC

    Well yeah, but aren't you just back to square one?

    C:\test>perl -nwle"print $n = +(split)" Use of implicit split to @_ is deprecated at -e line 1. Name "main::n" used only once: possible typo at -e line 1. foo 1 foo bar 2

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      D'Oh! I forgot to -w when I tested it. It's incredible how annoying this little thing can be!!! All in all I would regard the assignment to @_ and the connected warning as spurious, since split is not called in void context. But... they're there!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://547114]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2021-01-23 12:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?