Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

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


in reply to Counting the number of items returned by split without using a named array

The so called "goatse opearator":

=()=

BTW: if you don't know what goatse is, then chances are you don't want to!

Incidentally, dou you really want \s+? The default, which is ' ' is a special case and does what you mean in the vast majority of cases.

Replies are listed 'Best First'.
Re^2: Counting the number of items returned by split without using a named array
by salva (Abbot) on May 03, 2006 at 10:34 UTC
    the =()= trick can not be used with split as it is "optimized" into split /foo/, $bar, 1::
    $ perl -MO=Deparse -e '$a = () = split' $a = () = split(" ", $_, 1);

      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
        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');

        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.
      pretty amazing, that "optimization":
      $ perl -MO=Deparse -e '$a = () = split(" ",$_,0)' $a = () = split(" ", $_, 1);

      BUT:
      $ perl -MO=Deparse -e '$a = () = split(" ",$_,100000)' $a = () = split(" ", $_, 100000);

      so it's not completely impossible to use it with split, just limited to a fixed maximum number of fields in the split.
      It's a bit unsuspected (at least for me) though, that an explicit  split(" ",$_,0) was 'optimized' also.

      Btw - just out of curiosity put this version in the benchmark also - and it's faster than the regexp version, but still slower than  $n = @{[ split ]};

      I.

Re^2: Counting the number of items returned by split without using a named array
by Anonymous Monk on May 03, 2006 at 12:55 UTC
    a) You're right, I didnt want to know that

    b) hm, I just wanted to catch spaces and tabs and thought \s+ most appropriate.

    Never seen =()= in the perldoc before :-/
    I.

      Because it's not an operator of itself. It's an assignment to a list further piped into another assignment. It's just a means to create a list context. Others may find a better wording to describe it: possibly mine is not as technically accurate as it could be. Unfortunately as others already explained, it's not reliable to use it with split.

Re^2: Counting the number of items returned by split without using a named array
by Anonymous Monk on May 03, 2006 at 17:45 UTC
    Hm, sorry to have asked, as I see the two prominent solutions to the problem had already been discussed here
    (hope linking works as expected now, preview was fine... if not: was meant to link here http://www.perlmonks.org/?node_id=527973) I.

      You should not be sorry. Even if the topic seems trivial and elementary, it turned out to be more complex than one would probably think, and thus the discussion has been very interesting.

      BTW: to insert a link you should use [id://527973] or [id://527973|here], which render like Perl Idioms Explained - my $count = () = /.../g and here respectively. This is the preferred way since they will bring up the correct link both if you're in http://perlmonks.org and http://www.perlmonks.org, or any other possible mirror. See this node for more info.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://547104]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2018-07-20 03:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (423 votes). Check out past polls.

    Notices?