|
2016-03-06
| ||
| 15:25 | • Closed ticket [a95309bf70]: "lselect" to form a list from an existing ... artifact: 976d13e49f user: anonymous | |
|
2014-12-11
| ||
| 10:43 | • Ticket [a95309bf70]: 4 changes artifact: d72d074b1c user: dkf | |
|
2014-12-04
| ||
| 17:25 | • Ticket [a95309bf70]: 3 changes artifact: ef5deccdd0 user: crshults | |
| 17:01 | • Ticket [a95309bf70]: 3 changes artifact: 092d5c4cb1 user: crshults | |
|
2014-01-13
| ||
| 19:39 | • Ticket [a95309bf70]: 4 changes artifact: 05d9191056 user: dkf | |
|
2014-01-12
| ||
| 00:50 | • New ticket [a95309bf70]. artifact: 2dac72d826 user: anonymous | |
"lselect" to form a list from an existing list
Closeda95309bf7057f8486a3aff8be658460631984d26Created 13 years agoUpdated 11 years ago
Descriptionplain2014-01-12 00:50:14
~ Prototype
lselect list element1 ?element2 ...?
~ Example
set l {a b c}
lselect $l 0 2 # a c
lselect $l 0 # a
lselect $l 0 1 2 # a b c
lselect $l 4 # {} according to [lindex]
~ One implementation
proc lselect {l args} {
foreach index $args {
lappend result [lindex $l $index]
}
return $result
}
dkf Ticket [a95309bf70]: 4 changes
[History diff]2014-01-13 19:39:41
crshultsplain2014-12-04 17:01:39
This is [lindex] for the user who wants to retrieve multiple indices from the source list, so I think the name should be [lindices]
Implementation:
proc lindices {the_list args} {
if {[llength $args]} {
set result {}
foreach index [join $args] {
if {$index < [llength $the_list]} {
lappend result [lindex $the_list $index]
}
}
return $result
}
return $the_list
}
Example Usage:
lindices {a b c} # returns the original list as lindex does
lindices {a b c} 0
lindices {a b c} 1
lindices {a b c} 3 # returns nothing as lindex does
lindices {a b c} 0 1 3
lindices {a b c} {0 1 3} # args list can be a list
My opinion is this is something simple enough for the user to implement in Tcl and place in their personal library/toolbox. I keep mine in lib/tcl/8.6/list_tools-1.0.0.tm along with others like [lremove] [lrotate] [lpop] etc.
crshultsplain2014-12-04 17:25:24
Updated implementation to stay a little more true to [lindex] behaviour:
proc lindices {the_list args} {
if {[llength $args]} {
set result {}
foreach index [join $args] {
if {[string is integer $index]} {
if {$index >= 0 && $index < [llength $the_list]} {
lappend result [lindex $the_list $index]
}
} else {
lappend result [lindex $the_list $index]
}
}
return $result
}
return $the_list
}
Updated Example Usage:
lindices {a b c}; # returns the original list as lindex does
lindices {a b c} 0;
lindices {a b c} 1;
lindices {a b c} 3; # returns nothing as lindex does
lindices {a b c} 0 1 3;
lindices {a b c} {0 1 3}; # args list can be a list
lindices {a b c} -1; # returns nothing as lindex does
lindices {a b c} x; # bad index "x": must be integer?[+-]integer? or end?[+-]integer?
lindices {a b c} {0 1 end 0 3 end-1};
dkfhtml2014-12-11 10:43:33
Would this be a reasonable implementation?
proc lselect {list args} {
lmap index $args {lindex $list $index}
}
Remember that this will support lists of indices too. Cut-n-paste from a session:
% lselect {a {b c {d e}} f {g h}} {1 1} 1 3 {1 2 0}
c {b c {d e}} {g h} d
anonymousplain2016-03-06 15:25:55
I'm the reporter. Since this is not quite appropriate for the Tcl core, I'm closing this ticket.
Log in (anonymous is fine) to add a comment.