[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)