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"