| Ticket UUID: | edebb6a4ff741f7e7ddcaa17355640a586b52502 | ||
| Title: | lsearch -index requires nested items to exist | ||
| Type: | Bug | Created on: | 2024-11-08 11:45:39 |
| Submitter: | pooryorick | Assigned to: | nobody |
| Subsystem: | - New Builtin Commands | Severity: | Minor |
| Priority: | 5 Medium | Last modified: | 2024-11-10 22:16:34 |
| Status: | Open | Closed by: | nobody |
| Resolution: | None | Closed on: | |
| Version: | |||
| Description: | ||||
|
The following command,
Fails with the error
lsearch would be more useful if the meaning of "-index" was just "match against each value that has such an item", without the additional "each value must have such an item" constraint. A script that uses lsearch is usually more interested in determing which items match, not ensuring that all items meet some criteria. | ||||
| User Comments: | ||||
juliannoble2 added on 2024-11-09 18:26:19:
For working with flat lists - this can sometimes be a bit annoying/unintuitive.
% lsearch -stride 3 -index {0 0} -subindices -all -inline {f1 a1 a2 f2 b1 ""} *
f1 f2
% lsearch -stride 3 -index {1 0} -subindices -all -inline {f1 a1 a2 f2 b1 ""} *
a1 b1
% lsearch -stride 3 -index {2 0} -subindices -all -inline {f1 a1 a2 f2 b1 ""} *
element 0 missing from sublist ""
Usually I'd just reconfigure the datastructure to use lists of lists - but there are some things for which a flat list is more suited.
e.g in console work; for outputting a list of words in columns - where the number of columns might change based on available display width.
proc filler_count {listlen numcolumns} {
expr {($numcolumns - ($listlen % $numcolumns)) % $numcolumns}
}
proc col {list numcolumns colindex {blank NULL}} {
lsearch -stride $numcolumns -index [list $colindex 0] -subindices -all -inline [list {*}$list {*}[lrepeat [filler_count [llength $list] $numcolumns] $blank]] *
}
#... remainder of block display code omitted.
%col {a bunch of words to put in a grid etc} 3 0 ;#retrieve the first column of the words arranged into a total of 3 columns so you can determine the width required for the column.
a words in etc
This will fail if the wordlist contains an empty string, or if you set the 'blank' argument to empty string.
It can be worked around by using {""} or some other character for empty-string words in the list and for the 'blank' argument - or of course just by using something other than the lsearch -stride mechanism to build the lists.
It does seem something of a pity this doesn't work as might first be expected - but I guess the trade-off might be due to maintaining the ability to differentiate empty string values vs missing items, or in aiding error detection in list structures?
oehhar added on 2024-11-10 14:58:41:
IMHO the requirement that a not existing element can never be found would be a good idea. It is more tcl-like than erroring out. Any other comments on this ? Thanks, Harald griffin added on 2024-11-10 16:05:02:
Is an empty string a list? The answer is yes. Therefore, this scenario should not be an error. The value of "lindex {} 0" is {}, which is a valid list. pooryorick added on 2024-11-10 22:16:34:
It's not the same case as
The result should be -1, not 3. The absence of an item at index zero should not compare equal to the empty string. | ||||
