• Some list behavior I can't understand

    From Luc@21:1/5 to All on Sat Nov 26 23:05:25 2022
    I want to parse a list of numbers and find the one that is
    the closest to a target number:


    proc closest {target args} {
    set closest 9999999999999999999
    puts $args
    foreach i $args {
    puts "i is $i"
    if {$i > $target} {set diff [expr $i - $target]}
    if {$target > $i} {set diff [expr $target - $i]}
    if {$diff < $closest} {set closest $diff; set ret $i}
    }
    return $ret
    }


    Testing on Tkcon:

    % closest 456 [list 45 256 987 599 362]
    args is {45 256 987 599 362}
    i is 45 256 987 599 362
    missing operator at _@_
    in expression "456 - 45 _@_256 987 599 362"

    Well, so $args is a list. But $i is not treated as an item
    of the list in the foreach loop. Why not?


    Let's try again:

    proc closest {target args} {
    set closest 9999999999999999999
    puts $args
    foreach i {*}$args {
    puts "i is $i"
    if {$i > $target} {set diff [expr $i - $target]}
    if {$target > $i} {set diff [expr $target - $i]}
    if {$diff < $closest} {set closest $diff; set ret $i}
    }
    return $ret
    }

    % closest 456 [list 45 256 987 599 362]
    args is {45 256 987 599 362}
    i is 45
    i is 256
    i is 987
    i is 599
    i is 362
    362

    It works with the nipple! Why?

    --
    Luc


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael Soyka@21:1/5 to Michael Soyka on Sat Nov 26 22:07:05 2022
    On 11/26/2022 9:59 PM, Michael Soyka wrote:
    Hi Luc,

    Lookup the documentation for "proc" and you'll see that "args" when used
    as the last argument is special.

    -mike

    I meant to add that in your case, "args" is a list of lists:
    { {45 256 ...} }


    On 11/26/2022 9:05 PM, Luc wrote:
    I want to parse a list of numbers and find the one that is
    the closest to a target number:


    proc closest {target args}    {
        set closest 9999999999999999999
        puts $args
        foreach i $args    {
            puts "i is $i"
            if {$i     > $target}    {set diff [expr $i - $target]}
            if {$target > $i}        {set diff [expr $target - $i]}
            if {$diff < $closest}    {set closest $diff; set ret $i} >>     }
        return $ret
    }


    Testing on Tkcon:

    % closest 456 [list 45 256 987 599 362]
    args is {45 256 987 599 362}
    i is 45 256 987 599 362
    missing operator at _@_
    in expression "456 - 45 _@_256 987 599 362"

    Well, so $args is a list. But $i is not treated as an item
    of the list in the foreach loop. Why not?


    Let's try again:

    proc closest {target args}    {
        set closest 9999999999999999999
        puts $args
        foreach i {*}$args    {
            puts "i is $i"
            if {$i     > $target}    {set diff [expr $i - $target]}
            if {$target > $i}        {set diff [expr $target - $i]}
            if {$diff < $closest}    {set closest $diff; set ret $i} >>     }
        return $ret
    }

    % closest 456 [list 45 256 987 599 362]
    args is {45 256 987 599 362}
    i is 45
    i is 256
    i is 987
    i is 599
    i is 362
    362

    It works with the nipple! Why?



    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael Soyka@21:1/5 to Luc on Sat Nov 26 21:59:18 2022
    Hi Luc,

    Lookup the documentation for "proc" and you'll see that "args" when used
    as the last argument is special.

    -mike

    On 11/26/2022 9:05 PM, Luc wrote:
    I want to parse a list of numbers and find the one that is
    the closest to a target number:


    proc closest {target args} {
    set closest 9999999999999999999
    puts $args
    foreach i $args {
    puts "i is $i"
    if {$i > $target} {set diff [expr $i - $target]}
    if {$target > $i} {set diff [expr $target - $i]}
    if {$diff < $closest} {set closest $diff; set ret $i}
    }
    return $ret
    }


    Testing on Tkcon:

    % closest 456 [list 45 256 987 599 362]
    args is {45 256 987 599 362}
    i is 45 256 987 599 362
    missing operator at _@_
    in expression "456 - 45 _@_256 987 599 362"

    Well, so $args is a list. But $i is not treated as an item
    of the list in the foreach loop. Why not?


    Let's try again:

    proc closest {target args} {
    set closest 9999999999999999999
    puts $args
    foreach i {*}$args {
    puts "i is $i"
    if {$i > $target} {set diff [expr $i - $target]}
    if {$target > $i} {set diff [expr $target - $i]}
    if {$diff < $closest} {set closest $diff; set ret $i}
    }
    return $ret
    }

    % closest 456 [list 45 256 987 599 362]
    args is {45 256 987 599 362}
    i is 45
    i is 256
    i is 987
    i is 599
    i is 362
    362

    It works with the nipple! Why?


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ralf Fassel@21:1/5 to All on Mon Nov 28 10:46:10 2022
    * Michael Soyka <[email protected]>
    | Hi Luc,

    | Lookup the documentation for "proc" and you'll see that "args" when
    | used as the last argument is special.

    After reading said documentation
    https://www.tcl.tk/man/tcl/TclCmd/proc.html
    you (Luc) will understand why

    closest 456 45 256 987 599 362

    closest 456 45 256 987 599 362 1 2 3 4 1000

    work as well with your original version.

    R'

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