Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"lsearch" missing "-boolean" flag

50 views
Skip to first unread message

aotto1968

unread,
Jul 20, 2022, 4:52:03 AM7/20/22
to

Hi,

*lsearch* search for a glob/regexp/string in a list and return the index
of the position.

> For a "in/out" test is this to mutch information:

example: the following code is a "in/out" test

if { [lsearch $::flags val] != -1 } {
.. in
} else {
.. out
}

This is good but if you put the *lsearch* in *tcl::mathfunc proc* you
have a problem:

namespace eval tcl::mathfunc {
proc hasFlag {val} {
expr {[lsearch $::flags $val] != -1}
}
}

and do finally :

if { hasFlag("val") } {
.. in
} else {
.. out
}

As you see that you have *TWO* *expr* first in the *mathfunc* and than
after in the *if*.

> A good solution would be a *-boolean* flag for "io/out" test

namespace eval tcl::mathfunc {
proc hasFlag {val} {
lsearch -boolean $::flags $val
}
}

mfg

Ralf Fassel

unread,
Jul 20, 2022, 5:51:03 AM7/20/22
to
* aotto1968 <aott...@t-online.de>
| > A good solution would be a *-boolean* flag for "io/out" test
>
| namespace eval tcl::mathfunc {
| proc hasFlag {val} {
| lsearch -boolean $::flags $val
| }
| }

In TCL 8.5ff this is

expr {$val in $::flags}

for the exact search (not glob or regexp).

R'

heinrichmartin

unread,
Jul 20, 2022, 7:01:44 PM7/20/22
to
On Wednesday, July 20, 2022 at 10:52:03 AM UTC+2, aotto1 wrote:
> As you see that you have *TWO* *expr* first in the *mathfunc* and than
> after in the *if*.

I am not sure whether I understand your concerns. Have you tested your claim?

expect:~$ proc ::tcl::mathfunc::hasFlag {val} {expr {-1 != [lsearch {a b c} $val]}}
expect:~$ expr {hasFlag("a")}
1
expect:~$ expr {hasFlag("d")}
0
expect:~$ proc ::tcl::mathfunc::hasFlag {val} {expr {$val in {a b c}}}

Both work for me.

Maybe your code at some point was: expr [lsearch $::flags $val] != -1 ;# i.e. without braces

expect:~$ proc ::tcl::mathfunc::hasFlag {val} {expr $val in {a b c}} ;# wrong!
expect:~$ expr {hasFlag("a")}
invalid bareword "a"
in expression "a in a b c";
should be "$a" or "{a}" or "a(...)" or ...
while evaluating {expr {hasFlag("a")}}

On Wednesday, July 20, 2022 at 11:51:03 AM UTC+2, Ralf Fassel wrote:
> In TCL 8.5ff this is
>
> expr {$val in $::flags}
>
> for the exact search (not glob or regexp).

And for strings only (which is the case in Otto's example), because in/ni create string representations for all list elements (up to the first match). [lsearch -integer] might still be the better choice when microseconds count ;-)
0 new messages