• Feature Request: lappend return index

    From Andreas Otto@21:1/5 to All on Thu Aug 11 02:01:29 2022
    https://wiki.tcl-lang.org/page/Feature+Request%3A+lappend+return+index

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From heinrichmartin@21:1/5 to All on Thu Aug 11 04:14:42 2022
    On Thursday, August 11, 2022 at 11:01:31 AM UTC+2, aotto1 wrote:
    https://wiki.tcl-lang.org/page/Feature+Request%3A+lappend+return+index

    Before others will point out that wiki is the wrong tool, let me appreciate that you took your time to create a description with use case and examples :-)

    Nevertheless, the specification is not complete as lappend can append more list items; which index should be returned?

    Then:
    * the index of the latest item is "end", i.e. {[lindex [lappend list $a] end] == $a}
    * llength is a cheap operation because of Tcl's internal representation, you can get the value before and after lappend (and that also makes the code more readable imo)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to heinrichmartin on Thu Aug 11 13:15:48 2022
    heinrichmartin <martin.heinrich@frequentis.com> wrote:
    On Thursday, August 11, 2022 at 11:01:31 AM UTC+2, aotto1 wrote:
    https://wiki.tcl-lang.org/page/Feature+Request%3A+lappend+return+index

    Before others will point out that wiki is the wrong tool, let me
    appreciate that you took your time to create a description with use
    case and examples :-)

    Nevertheless, the specification is not complete as lappend can append
    more list items; which index should be returned?

    Then:

    * the index of the latest item is "end", i.e. {[lindex [lappend list
    $a] end] == $a}

    * llength is a cheap operation because of Tcl's internal
    representation, you can get the value before and after lappend (and
    that also makes the code more readable imo)

    Further, for the specific case requested in the wiki article, there is
    no need to maintain a separate variable, in the case of wanting the
    index of the end element after lappend:

    The LcConfigC_WriteHDL method just needs to be:

    method LcConfigC_WriteHDL { hdl } {
    my variable LcConfigC_Handle
    lappend LcConfigC_Handle $hdl
    return [expr {[llength $LcConfigC_Handle]-1}]
    }

    To get the index of the new "end" element.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Heller@21:1/5 to Rich on Thu Aug 11 14:08:34 2022
    At Thu, 11 Aug 2022 13:15:48 -0000 (UTC) Rich <rich@example.invalid> wrote:


    heinrichmartin <martin.heinrich@frequentis.com> wrote:
    On Thursday, August 11, 2022 at 11:01:31 AM UTC+2, aotto1 wrote:
    https://wiki.tcl-lang.org/page/Feature+Request%3A+lappend+return+index

    Before others will point out that wiki is the wrong tool, let me
    appreciate that you took your time to create a description with use
    case and examples :-)

    Nevertheless, the specification is not complete as lappend can append
    more list items; which index should be returned?

    Then:

    * the index of the latest item is "end", i.e. {[lindex [lappend list
    $a] end] == $a}

    * llength is a cheap operation because of Tcl's internal
    representation, you can get the value before and after lappend (and
    that also makes the code more readable imo)

    Further, for the specific case requested in the wiki article, there is
    no need to maintain a separate variable, in the case of wanting the
    index of the end element after lappend:

    The LcConfigC_WriteHDL method just needs to be:

    method LcConfigC_WriteHDL { hdl } {
    my variable LcConfigC_Handle
    lappend LcConfigC_Handle $hdl
    return [expr {[llength $LcConfigC_Handle]-1}]
    }

    To get the index of the new "end" element.

    Also: the index "end" is always the index of the last element of a list. The word end can be used anywhere a list index can be used:


    lappend foo a b c
    puts [lindex $foo end]





    --
    Robert Heller -- Cell: 413-658-7953 GV: 978-633-5364
    Deepwoods Software -- Custom Software Services
    http://www.deepsoft.com/ -- Linux Administration Services
    heller@deepsoft.com -- Webhosting Services

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to Robert Heller on Thu Aug 11 15:53:11 2022
    Robert Heller <heller@deepsoft.com> wrote:
    At Thu, 11 Aug 2022 13:15:48 -0000 (UTC) Rich <rich@example.invalid> wrote:


    heinrichmartin <martin.heinrich@frequentis.com> wrote:
    On Thursday, August 11, 2022 at 11:01:31 AM UTC+2, aotto1 wrote:
    https://wiki.tcl-lang.org/page/Feature+Request%3A+lappend+return+index

    Before others will point out that wiki is the wrong tool, let me
    appreciate that you took your time to create a description with use
    case and examples :-)

    Nevertheless, the specification is not complete as lappend can append
    more list items; which index should be returned?

    Then:

    * the index of the latest item is "end", i.e. {[lindex [lappend list
    $a] end] == $a}

    * llength is a cheap operation because of Tcl's internal
    representation, you can get the value before and after lappend (and
    that also makes the code more readable imo)

    Further, for the specific case requested in the wiki article, there is
    no need to maintain a separate variable, in the case of wanting the
    index of the end element after lappend:

    The LcConfigC_WriteHDL method just needs to be:

    method LcConfigC_WriteHDL { hdl } {
    my variable LcConfigC_Handle
    lappend LcConfigC_Handle $hdl
    return [expr {[llength $LcConfigC_Handle]-1}]
    }

    To get the index of the new "end" element.

    Also: the index "end" is always the index of the last element of a list. The word end can be used anywhere a list index can be used:


    lappend foo a b c
    puts [lindex $foo end]

    Yes, but the OP's posting (and the wiki article) appears to be using
    the numeric list position of the elements as a "handle" (in some ways
    similar to libc file handle integers) to reference whatever it is the
    list element references.

    So for his use case he wants the numeric value of the new "end" so when
    he attaches another new "end" his "handle" for the prior one does not
    change position within the list.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas Otto@21:1/5 to Robert Heller on Thu Aug 11 12:16:15 2022
    Hi, I want the *index* and *not* the *value* as return:

    only possible solution is `expr {[llength $LcConfigC_Handle]-1}` but this only works well if one argument is appended
    multiple arguments like `lappend -index varname a b c d` should return a list of indexes from position of "a b c d"


    Robert Heller schrieb am Donnerstag, 11. August 2022 um 16:09:45 UTC+2:
    At Thu, 11 Aug 2022 13:15:48 -0000 (UTC) Rich <ri...@example.invalid> wrote:


    Also: the index "end" is always the index of the last element of a list. The word end can be used anywhere a list index can be used:


    lappend foo a b c
    puts [lindex $foo end]





    --
    Robert Heller -- Cell: 413-658-7953 GV: 978-633-5364
    Deepwoods Software -- Custom Software Services
    http://www.deepsoft.com/ -- Linux Administration Services
    hel...@deepsoft.com -- Webhosting Services

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