in reply to Sort path strings by component count.

I fixed something like this the other day just by giving it an explict array to split to. The original code was something like:
my $word_count = int(split(' ', $string));
And I replaced it with:
my $word_count = scalar(my @words = split(' ', $string));
My first thought was that using "split" to count was inelegant, but there were two problems with getting away from it in this case.
One is that perl's m// doesn't give you a count of matches, and using s/// to count without changing the input $string would require something which is also rather inelegant:
$string =~ s/(\s+)/$1/g;
The other problem was that it's not so simple to precisely match split's ' ' special case, which ignores any leading or trailing spaces.
On balance, I thought that introducing a spurious array was my best option.

Replies are listed 'Best First'.
Re^2: Sort path strings by component count.
by Roy Johnson (Monsignor) on Feb 27, 2006 at 15:57 UTC