Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting occurrences of a substring on Tcl 8.0

Tags:

string

tcl

I'm trying to figure out how many times one string occurs in another, which Tcl doesn't seem to have built-in. I can't use either of the solutions on the wiki, since I have to support Tcl 8.0.

This doesn't work:

# needleString is known to contain no regex metacharacters
llength [regexp -all -inline $needleString $haystackString]

because -all isn't supported on 8.0.

This doesn't work:

proc string_occurrences {needleString haystackString} {

    set j [string first $needleString $haystackString 0]
    if {$j == -1} {return 0}

    set i 0
    while {$j != -1 } {
        set j [string first $needleString $haystackString [incr j]]
        incr i
    }

    return $i
}

because string first doesn't support the startIndex argument on 8.0.

I could modify string_occurrences to use string range to take substrings of the string and 2-argument string first to search inside those, but that's even more cumbersome than the loop already is, and I don't know how efficient string range is. Do I have any better options?

like image 739
user2357112 supports Monica Avatar asked Jan 20 '26 05:01

user2357112 supports Monica


1 Answers

You could use [regsub -all] (which is present in 8.0) to create a new string with the needleString removed, and compare lengths:

proc string_occurrences {needleString haystackString} {
    regsub -all $needleString $haystackString {} stripped
    expr {([string length $haystackString] - [string length $stripped]) / [string length $needleString]}
}
like image 153
evil otto Avatar answered Jan 23 '26 13:01

evil otto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!