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

Doesn't work. split is optimized into splitting into no more fields than one more than you request. See perldoc -f split:
When assigning to a list, if LIMIT is omitted, Perl supplies a LIMIT one larger than the number of variables in the list, to avoid unnecessary work.

Using ()= you request 0 fields, so in scalar context, you get 1.

print my $n = () = split /-/, 'a-b-c';
1
  • Comment on Re: Re: How to get the number of fields found by split without a warning?
  • Download Code

Replies are listed 'Best First'.
Re: Re: Re: How to get the number of fields found by split without a warning?
by PodMaster (Abbot) on Dec 08, 2003 at 21:28 UTC
    Still not a problem (but you knew that)(:
    E:\>perl -le"die $_ = () = split ',','a,b,c,d,e,f,g',-1" 7 at -e line 1.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Except that it won't drop trailing blank fields, as it should. If you don't care about this, you needn't use split.
      print $_ = () = split ',','a,b,c,,,,',-1
      7