Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by duff (Parson)
on Dec 08, 2003 at 21:08 UTC ( [id://313239]=note: print w/replies, xml ) Need Help??


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

If you're really determined to do this, you could use the =()= pseudo-operator:

my $n =()= split ',', $str;

Update: Quite right bart, I left out the 3rd arg to split(), the correct code is as follows.

my $n =()= split ',', $str, -1;

Replies are listed 'Best First'.
Re: Re: How to get the number of fields found by split without a warning?
by bart (Canon) on Dec 08, 2003 at 21:15 UTC
    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
    
      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
        
Re: Re: How to get the number of fields found by split without a warning?
by Not_a_Number (Prior) on Dec 08, 2003 at 21:17 UTC
    my $n =()= split ',', $str;

    er... that seems to give me 1, whatever the length of the string

    Am I missing something?

    dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-16 22:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found