Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: da1232507b53edc992d99029b4fcec7111e270e0337acb88f737454676d7ef27
Ticket: edebb6a4ff741f7e7ddcaa17355640a586b52502
lsearch -index requires nested items to exist
User & Date: juliannoble2 on 2024-11-09 18:26:19
Changes

  1. icomment:
    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?
    
  2. login: "juliannoble2"
  3. mimetype: "text/plain"