https://wiki.tcl-lang.org/page/Feature+Request%3A+lappend+return+index
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)
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.
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]
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
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 485 |
Nodes: | 16 (2 / 14) |
Uptime: | 131:40:43 |
Calls: | 9,655 |
Calls today: | 3 |
Files: | 13,707 |
Messages: | 6,166,572 |