A man <> wrote:
> How do I count the number of delimiters in a string when split drops
> any null fields at the end of a string? Example:
> My string=1,2,3,,4,5,,,,
>
> I want to count how many delimiters there are, which should be 8.
I make it 9 delimiters, or 10 fields.
> But if I do a split I get:
<snip>
> I need to count those other commas at the end. Does anyone know how
> to do this?
my $string = "1,2,3,,4,5,,,,";
print $string =~ tr/,//;
# prints 9, the number of delimiters
print scalar grep {1} split /,/, $string, -1;
# prints 10, the number of fields
# the reason for the grep {1} is to avoid calling split in scalar
# context which would clobber @_
Ben
--
Musica Dei donum optimi, trahit homines, trahit deos. |
Musica truces mollit animos, tristesque mentes erigit. |
Musica vel ipsas arbores et horridas movet feras. |