• "lsearch" missing "-boolean" flag

    From aotto1968@21:1/5 to All on Wed Jul 20 10:51:58 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ralf Fassel@21:1/5 to All on Wed Jul 20 11:50:58 2022
    * aotto1968 <aotto1968@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'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From heinrichmartin@21:1/5 to Ralf Fassel on Wed Jul 20 16:01:42 2022
    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 ;-)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)