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.
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 |