• Tablelist and sorting a column with empty cells by integer

    From greg@gregor.ebbing@gmx.de to comp.lang.tcl on Tue Apr 2 23:58:31 2024
    From Newsgroup: comp.lang.tcl

    Hello

    Tablelist 7.1
    Sorting a column with empty cells by integer:
    But I have an error. How do I do this correctly?

    Gregor


    if {0} { https://www.nemethi.de/tablelist/tablelistWidget.html#col_formatcommand
    ...
    proc formatNumber val { return [expr {$val == -1 ? "" : $val}] }

    This will make the cells displaying an empty string appear before
    all the others when sorting the column in increasing order. To achieve
    the opposite effect, you will have to replace the -1 with an application-specific value that is strictly greater than all the other
    numbers contained in the column in question.
    ...
    }

    package require Tk
    package require tablelist

    proc formatNumber val {
    return [expr {$val == -1 ? "" : $val}]
    }

    set tf [frame .fr]
    set tbl $tf.tbl
    set vsb $tf.vsb

    tablelist::tablelist $tbl -columns {0 Text left 0 "Number" right } \
    -yscrollcommand [list $vsb set] -width 0 \
    -labelcommand tablelist::sortByColumn

    set col 1
    $tbl columnconfigure $col -formatcommand formatNumber -sortmode integer

    scrollbar $vsb -orient vertical -command [list $tbl yview]
    grid $tbl -row 0 -column 0 -sticky news
    grid $vsb -row 0 -column 1 -sticky ns
    grid rowconfigure $tf 0 -weight 1
    grid columnconfigure $tf 0 -weight 1
    pack $tf -side top -expand yes -fill both

    # Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven 11}
    {empty ""}]

    if {0} {
    Output:
    expected integer but got ""
    expected integer but got ""
    while executing
    "tablelist::sortByColumn .fr.tbl 1"
    ("uplevel" body line 1)
    invoked from within
    "uplevel #0 $cmd [list $win $col] "
    (procedure "tablelist::labelB1Up" line 109)
    invoked from within
    "tablelist::labelB1Up .fr.tbl.hdr.t.f.l1 990"
    (command bound to event)

    }
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From greg@gregor.ebbing@gmx.de to comp.lang.tcl on Wed Apr 3 06:03:52 2024
    From Newsgroup: comp.lang.tcl

    Misconception on my part

    correct with

    #Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven 11}
    {empty -1}]



    for
    #Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven 11}
    {empty ""}]

    I have to write a sort command.

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From nemethi@csaba.nemethi@t-online.de to comp.lang.tcl on Wed Apr 3 11:26:14 2024
    From Newsgroup: comp.lang.tcl

    Am 03.04.24 um 06:03 schrieb greg:
    Misconception on my part

    correct with

    #Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven 11}
     {empty -1}]



    for
    #Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven 11}
     {empty ""}]


    This correction eliminates the error message because all *internal* cell values of the column "Number" are now integers.

    I have to write a sort command.


    Why? For the column "Number" no sort command is needed.
    --
    Csaba Nemethi https://www.nemethi.de mailto:csaba.nemethi@t-online.de

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From greg@gregor.ebbing@gmx.de to comp.lang.tcl on Wed Apr 3 20:56:22 2024
    From Newsgroup: comp.lang.tcl

    Am 03.04.24 um 11:26 schrieb nemethi:
    Am 03.04.24 um 06:03 schrieb greg:
    Misconception on my part

    correct with

    #Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven
    11}   {empty -1}]



    for
    #Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven
    11}   {empty ""}]


    This correction eliminates the error message because all *internal* cell values of the column "Number" are now integers.

    I have to write a sort command.


    Why?  For the column "Number" no sort command is needed.


    1)
    set col 1
    $tbl columnconfigure $col -formatcommand formatNumber
    # Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven 11}
    {empty -1}]

    sorts the 11 between 1 and 2

    2)
    set col 1
    $tbl columnconfigure $col -formatcommand formatNumber -sortmode integer
    # Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven 11}
    {empty -1}]

    #sorts ok



    But if I need the values from column Number for a summation, I cannot
    use this method. Then I have an incorrect sum with the substitute values
    for "".
    Hence a proc for it

    proc customCompare {a b} {
    # Treat empty cells as very large so they will be sorted at the end
    if {$a eq ""} {set a 999999}
    if {$b eq ""} {set b 999999}
    # Convert to numbers and compare
    set numA [expr {int($a)}]
    set numB [expr {int($b)}]
    if {$numA < $numB} {
    return -1
    } elseif {$numA > $numB} {
    returns 1
    } else {
    return 0
    }
    }



    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From nemethi@csaba.nemethi@t-online.de to comp.lang.tcl on Wed Apr 3 21:37:39 2024
    From Newsgroup: comp.lang.tcl

    Am 03.04.24 um 20:56 schrieb greg:
    Am 03.04.24 um 11:26 schrieb nemethi:
    Am 03.04.24 um 06:03 schrieb greg:
    Misconception on my part

    correct with

    #Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven
    11}   {empty -1}]



    for
    #Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven
    11}   {empty ""}]


    This correction eliminates the error message because all *internal*
    cell values of the column "Number" are now integers.

    I have to write a sort command.


    Why?  For the column "Number" no sort command is needed.


    1)
    set col 1
    $tbl columnconfigure $col -formatcommand formatNumber
    # Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven 11}
     {empty -1}]

    sorts the 11 between 1 and 2

    2)
    set col 1
    $tbl columnconfigure $col -formatcommand formatNumber -sortmode integer
    # Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven 11}
     {empty -1}]

    #sorts ok



    But if I need the values from column Number for a summation, I cannot
    use this method. Then I have an incorrect sum with the substitute values
    for "".
    Hence a proc for it

    proc customCompare {a b} {
    # Treat empty cells as very large so they will be sorted at the end
     if {$a eq ""} {set a 999999}
     if {$b eq ""} {set b 999999}
    # Convert to numbers and compare
     set numA [expr {int($a)}]
     set numB [expr {int($b)}]
     if {$numA < $numB} {
       return -1
     } elseif {$numA > $numB} {
       returns 1
     } else {
      return 0
     }
    }




    Why not use a summation proc rather than a comparison one? You could
    write a simple proc that adds the internal cell values, except that it
    skips the value -1 (or 999999) when building the sum.
    --
    Csaba Nemethi https://www.nemethi.de mailto:csaba.nemethi@t-online.de

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From greg@gregor.ebbing@gmx.de to comp.lang.tcl on Thu Apr 4 05:57:03 2024
    From Newsgroup: comp.lang.tcl

    Am 03.04.24 um 21:37 schrieb nemethi:
    Am 03.04.24 um 20:56 schrieb greg:
    Am 03.04.24 um 11:26 schrieb nemethi:
    Am 03.04.24 um 06:03 schrieb greg:
    Misconception on my part

    correct with

    #Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven
    11}   {empty -1}]



    for
    #Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven
    11}   {empty ""}]


    This correction eliminates the error message because all *internal*
    cell values of the column "Number" are now integers.

    I have to write a sort command.


    Why?  For the column "Number" no sort command is needed.


    1)
    set col 1
    $tbl columnconfigure $col -formatcommand formatNumber
    # Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven
    11}   {empty -1}]

    sorts the 11 between 1 and 2

    2)
    set col 1
    $tbl columnconfigure $col -formatcommand formatNumber -sortmode integer
    # Data
    $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven
    11}   {empty -1}]

    #sorts ok



    But if I need the values from column Number for a summation, I cannot
    use this method. Then I have an incorrect sum with the substitute
    values for "".
    Hence a proc for it

    proc customCompare {a b} {
    # Treat empty cells as very large so they will be sorted at the end
      if {$a eq ""} {set a 999999}
      if {$b eq ""} {set b 999999}
    # Convert to numbers and compare
      set numA [expr {int($a)}]
      set numB [expr {int($b)}]
      if {$numA < $numB} {
        return -1
      } elseif {$numA > $numB} {
        returns 1
      } else {
       return 0
      }
    }




    Why not use a summation proc rather than a comparison one?  You could
    write a simple proc that adds the internal cell values, except that it
    skips the value -1 (or 999999) when building the sum.

    That approach didn't even occur to me.
    Thanks! Is also a solution


    Gregor
    Every now and then I have
    "ein Brett vor dem Kopf".
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Ralf Fassel@ralfixx@gmx.de to comp.lang.tcl on Thu Apr 4 11:46:56 2024
    From Newsgroup: comp.lang.tcl

    * greg <gregor.ebbing@gmx.de>
    | 1)
    | set col 1
    | $tbl columnconfigure $col -formatcommand formatNumber
    | # Data
    | $tbl insertlist end [list {one 1} {two 2} {three 3} {four 4} {eleven
    | 11} {empty -1}]

    | sorts the 11 between 1 and 2

    [-sortmode dictionary] might take care of that, plus one would not
    (I *think*) require the special value -1. But even then, a separate
    summation command would be required to take care of the {empty ""}
    cells.

    R'
    --- Synchronet 3.20a-Linux NewsLink 1.114