http://www.perlmonks.org?node_id=547150


in reply to Re^2: Counting the number of items returned by split without using a named array
in thread Counting the number of items returned by split without using a named array

That fails when input has leading or trailing blanks:

$ perl -lpe '($_=()=/\s+/g)++' ## leading foo bar 3 $ perl -lpe '($_=()=/\s+/g)++' ## trailing foo bar 3 $ perl -lpe '($_=()=/\s+/g)++' ## both foo bar 4 $ _

It's better to count ocurrences of actual elements (\S+):

$ perl -wle 'print scalar (()=/\S+/g) for "a b c", " a b c", "a b c ", + " a b c "' 3 3 3 3 $ _

Anyway I prefer split too, like salva++'s 0e0 solution.

--
David Serrano