Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by talexb (Chancellor)
on Dec 08, 2003 at 20:55 UTC ( [id://313231]=note: print w/replies, xml ) Need Help??


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

As you've probably read, split returns an array, so I'd recommend you take advantage of that and afterwards count the number of elements in the array.

my $str = "a,b,c,d,e"; my @n = split ',', $str; print "There were " . ($#n+1) . " elements.\n";

The '+1' is required because array offsets start at zero.

--t. alex
Life is short: get busy!

Update: Or, as Corion pointed out, scalar(@n) which is much clearer.

Replies are listed 'Best First'.
Re: Re: How to get the number of fields found by split without a warning?
by Art_XIV (Hermit) on Dec 08, 2003 at 21:07 UTC

    Dittos to talexb, but I like scalar:

    use strict; my $str = "a,b,c,d,e"; my @n = split /,/, $str; print scalar @n, "\n"
    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-03-28 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found