Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by BrowserUk (Pope)
on May 03, 2006 at 10:16 UTC ( #547107=note: print w/replies, xml ) Need Help??


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

Another way that avoids a named array.

$entries = @{[ split /\s+/ ]};

Also, split /\s+/ is the similar to as the slightly magical split ' ', except undefs from leading whitespace are suppressed.

In turn, split ' ' is the same as split with no arguments, so you could reduce your code to:

$entries = @{[ split ]};

If you don't have leading whitespace, or don't want to count the undef any leading whitespace would produce as an entry.


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.

Replies are listed 'Best First'.
Re^2: Counting the number of items returned by split without using a named array
by Anonymous Monk on May 03, 2006 at 13:34 UTC
    My bias goes towards either
    $entries = @{[ split ]};

    or
    $entries = () = /\S+/g;

    as "most elegant".
    It's both sufficiently short, although I'm not sure which one is easier to understand for the uninitiated reader :)
    Just out of curiosity (it doesnt really matter in my case):
    which one would be the more (CPU- and memory-) efficient one?

    I.

      C:\test>p1 our $s = join ' ', 'aa'..'zz';; cmpthese -3, { split => q[ $_=$s; my $n = @{[ split ]}; ], regex => q[ $_=$s; my $n = () = /\S+/g; ] };; Rate regex split regex 546/s -- -50% split 1102/s 102% --

      Assuming I didn't goof on the benchmark split appears to be quicker.


      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.
        Strange
        perhaps it depends on the length of the input string?
        My attempt shows a different result
        You seem to be under windows - so just for comparison:
        $ cat a.pl; echo '-----------' ;echo;./a.pl #!/usr/local/bin/perl -w use Benchmark qw(:all) ; our $s = join ' ', 'aa'..'zz';; cmpthese( -3, { split => q[ $_=$s; my $n = @{[ split ]}; ], regex => q[ $_=$s; my $n = () = /\S+/g; ] }); ----------- Rate regex split regex 981/s -- -53% split 2098/s 114% --

        Hm, now where's the big diff to the other Bench?
        I.
      Just out of curiosity (it doesnt really matter in my case): which one would be the more (CPU- and memory-) efficient one?

      It hardly ever matters. As a wild guess I would say that since the former involves doing something and then undoing it and that something is taking a reference, it is more computationally intensive. In case of doubt

      use Benchmark;

      I may well (and happily!) prove wrong...

        As you wish (or not):
        $ cat foo.pl ; echo "--------";echo; ./foo.pl #!/usr/bin/perl ################## use Benchmark qw(:all) ; sub test1(){ $entries = @{[ split ]}; } sub test2(){ $entries = () = /\S+/g; } $_="This is an example string with several words bla bla bla\n"; $count=1E+7; timethis ($count, "test1()"); print "------------\n"; timethis ($count, "test2()"); -------- timethis 10000000: 13 wallclock secs (12.37 usr + 0.01 sys = 12.38 CP +U) @ 807754.44/s (n=10000000) ------------ timethis 10000000: 7 wallclock secs ( 6.66 usr + 0.00 sys = 6.66 CP +U) @ 1501501.50/s (n=10000000)
Re^2: Counting the number of items returned by split without using a named array
by blazar (Canon) on May 03, 2006 at 10:57 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2021-01-19 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?