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

Re: How to get the number of fields found by split without a warning?

by Rudif (Hermit)
on Dec 08, 2003 at 23:48 UTC ( #313310=note: print w/replies, xml ) Need Help??

in reply to How to get the number of fields found by split without a warning?

Many thanks to all who responed.

I will keep my second solution

$n = @{[ split ',', $str ]}; print "$n\n";
which was seconded above by Zed_Lopez.


  • Comment on Re: How to get the number of fields found by split without a warning?
  • Download Code

Replies are listed 'Best First'.
Re: Re: How to get the number of fields found by split without a warning?
by Roger (Parson) on Dec 09, 2003 at 02:37 UTC
    Why make another copy of the array with @{[ ... ]}? I would rather do this instead -
    $n = scalar( split ',', $str ); print "$n\n";
    Ok, lets give it a test -
    use strict; use Benchmark qw/timethese cmpthese/; my $str = join ',',map{('a'..'z')[rand 26] x (1 + rand 5)}0..1000; cmpthese( timethese(10000, { 'split_anonymous' => '&split_anonymous', 'split_simple' => '&split_simple', }) ); sub split_anonymous { my $n = @{[split ',', $str]}; #print "$n\n"; } sub split_simple { my $n = scalar split ',', $str; #print "$n\n"; }
    And the comparison -
    Benchmark: timing 10000 iterations of split_anonymous, split_simple... split_anonymous: 19 wallclock secs (19.05 CPU) @ 525.02/s (n=10000) split_simple: 16 wallclock secs (15.77 CPU) @ 634.32/s (n=10000) Rate split_anonymous split_simple split_anonymous 525/s -- -17% split_simple 634/s 21% --
    The benchmark result show that a simple split without making an anonymous copy is 20% faster.

      As davido points out, my question is : how to get rid of the warning.

      And in as few keystrokes as possible, without a significant time overhead.

      Finally, I decided to try the obvious solution : turn off warnings locally.

      sub split_simple_quiet { no warnings; my $n = scalar split ',', $str; #print "$n\n"; }
      This does the job and costs almost nothing in time, as the expanded benchmark shows:
      Use of implicit split to @_ is deprecated at H:\dev\perl\perlmonks\ben line 23. Benchmark: timing 10000 iterations of split_anonymous, split_simple, s +plit_simple_quiet... split_anonymous: 24 wallclock secs (15.55 usr + 0.00 sys = 15.55 CPU) + @ 643.00/s (n=10000) split_simple: 20 wallclock secs (12.23 usr + 0.00 sys = 12.23 CPU) @ +817.80/s (n=10000) split_simple_quiet: 18 wallclock secs (12.15 usr + 0.01 sys = 12.16 C +PU) @ 822.57/s (n=10000) Rate split_anonymous split_simple split_si +mple_quiet split_anonymous 643/s -- -21% + -22% split_simple 818/s 27% -- + -1% split_simple_quiet 823/s 28% 1% + --
      I like Roger's  my $qty = scalar ( split ',', $string ); method, however, it takes us back to where we started, by generating a warning message about implicit splitting into @_ being deprecated (if the code is tested under -w or use warnings;).

      Unfortunately, the solution is either to turn off warnings for that section of code, or to revert back to the other solution which makes an extra copy:  my $qty = @{[ split ',', $string ]};.

      Of course, if all you're trying to do it count commas except for trailing ones, you could probably just loop through the string instead.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2023-09-23 17:54 GMT
Find Nodes?
    Voting Booth?

    No recent polls found