TCL question: count occurrence of a character ...

Rainer Müller raimue at macports.org
Tue Dec 10 07:34:42 PST 2013


On 2013-12-10 16:18, Peter Danecek wrote:
> I would like to count the occurrences of a character, more specifically of the `.` in the ${version} option. Asking Big Brother Google, there seem to be quite some possibilities, so I came up with the following solutions:
> 
> set dot_count       [ regsub -all "\\." ${version} {} ignore ]

You won't need the substitution, so regexp should do.

  set dot_count [regexp -all \\. $version]

Another way would be splitting on the delimiter and count the elements.
Of course, for the actual number of dots you need to decrement the
result by one.

  set dot_count [expr [llength [split $version .]] - 1]

Rainer


More information about the macports-dev mailing list