• lsearch missing "-stride" option

    From aotto1968@21:1/5 to All on Fri Apr 29 09:32:58 2022
    Hi,

    I want to use "lsearch" to filter an "array get XXX" output.

    array get output return … key1 lst1 ke2 lst2… if "array" values are
    lists. The "-stride" option in "lsort" treat the FIRST level of output
    as "conacte" list. my example would do this with "lsearch"

    search -all -inline -stride 2 -index {1 0} {a {aa aaa} b {bb bbb} c {aa
    ccc} aa

    a {aa aaa} c {aa ccc}

    thanks.

    mfg.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From apn@21:1/5 to All on Fri Apr 29 15:50:05 2022
    On 4/29/2022 1:02 PM, aotto1968 wrote:
    Hi,

    I want to use "lsearch" to filter an "array get XXX" output.

    array get output return … key1 lst1 ke2 lst2… if "array" values are lists. The "-stride" option in "lsort" treat the FIRST level of output
    as "conacte" list. my example would do this with "lsearch"

    search -all -inline -stride 2 -index {1 0} {a {aa aaa} b {bb bbb} c {aa
    ccc} aa

    a {aa aaa} c {aa ccc}

    thanks.

    mfg.

    Implemented in 8.7 - see https://www.magicsplat.com/blog/tcl87-lists/
    for an example.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alexandru@21:1/5 to [email protected] on Sun May 1 00:55:17 2022
    [email protected] schrieb am Freitag, 29. April 2022 um 09:33:03 UTC+2:
    Hi,

    I want to use "lsearch" to filter an "array get XXX" output.

    array get output return … key1 lst1 ke2 lst2… if "array" values are lists. The "-stride" option in "lsort" treat the FIRST level of output
    as "conacte" list. my example would do this with "lsearch"

    search -all -inline -stride 2 -index {1 0} {a {aa aaa} b {bb bbb} c {aa
    ccc} aa

    a {aa aaa} c {aa ccc}

    thanks.

    mfg.
    Since Tcl8.7 is still alpha, here is an implementation in pure Tcl.
    You can use Lsearch just as you would use lsearch.

    ## Pure TCL implementation of the -stride option for lsearch command.
    # \param args arguments as for Tcl command lsearch
    # \return result as lsearch command
    proc Lsearch {args} {
    # If there are no options
    if {[llength $args]<=2} {
    return [lsearch {*}$args]
    }
    # Get the preceding options
    set opts [lrange $args 0 end-2]
    # Get the position of the -stride option
    set idx [lsearch $opts -stride]
    # If stride option is not available
    if {$idx<0} {
    # Execute standard lsearch command
    return [lsearch {*}$args]
    # If stride option is available
    } else {
    # Extract last two list arguments, which are the list and the searched string
    lassign [lrange $args end-1 end] l searchstring
    # Get the stride length, which is the value following the -stride option
    set stridelen [lindex $opts [expr {$idx+1}]]
    # Compose a nested list with the length of the sublists equal to the stride length
    set searchlist [ListGroups $l $stridelen]
    # Remove -stride option
    set idx [lsearch $opts -stride]
    set opts [lreplace $opts $idx [expr {$idx+1}]]
    # Execute the standard Tcl command with remaining options
    return [lsearch {*}$opts $searchlist $searchstring]
    }
    }
    ## Group elements of list l into groups of n elements.
    # see http://wiki.tcl.tk/440#pagetoc47290fd7
    proc ListGroups {l n} {
    set result [list]
    set len [llength $l]
    for {set p 0} {$p < $len} {incr p $n} {
    lappend result [lrange $l $p [expr {$p+$n-1}]]
    }
    return $result
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Schelte@21:1/5 to All on Sun May 1 12:41:48 2022
    On 29/04/2022 09:32, aotto1968 wrote:
    I want to use "lsearch" to filter an "array get XXX" output.

    Considering that the output of [array get XXX] is a valid dict, you can
    use dict filtering on it to get the desired result:

    dict filter [array get XXX] script {k v} {string match aa [lindex $v 0]}


    Schelte.

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