0

私は Tcl/Tk で書いているプログラムに「検索」機能を実装する過程にあり、このコードを書くための簡潔で効率的な方法を見つけるのに苦労しています。検索には、「すべてを検索」、「完全一致」、「上/下を検索」などのさまざまなオプションがあります。

これが私が試したことであり、私がここでしようとしていることです:

set idx [.text index insert];
set search_for $::search_entry;

set parameters "";
if {$::match_exact == 1} {append parameters "-exact "};
if {$::case_sensitive == 0} {append parameters "-nocase "};
if {$::find_all == 1} {append parameters "-all "};
if {$::direction == 1} {
    append parameters "-backwards";
} else {
    append parameters "-forewards";
}

.text search $parameters $search_for $idx;

これにより、検索しようとすると次のエラーが表示されます。

bad switch "-nocase -forewards": must be --, -all, -backwards, -count, -elide, -exact, -forwards, -nocase, -nolinestop, -overlap, -regexp, or -strictlimits

これを書くための同様の簡潔な方法があることを教えてください。ユーザーが設定したオプションの組み合わせごとに異なる検索を作成する必要があるのではないかと心配しています...

4

1 に答える 1

2

パラメータを文字列として定義しています。検索コマンドを呼び出すときに展開するリストである必要があります。次のようになります。

set parameters [list]
if {$::match_exact == 1} {lappend parameters "-exact"}
...
.text search {*}$parameters $search_for $idx
于 2014-07-20T20:27:09.477 に答える