Tcl Source Code

View Ticket
Login
Ticket UUID: a95309bf7057f8486a3aff8be658460631984d26
Title: "lselect" to form a list from an existing list
Type: RFE Version:
Submitter: anonymous Created on: 2014-01-12 00:50:14
Subsystem: 17. Commands I-L Assigned To: dkf
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2016-03-06 15:25:55
Resolution: None Closed By: anonymous
    Closed on: 2016-03-06 15:25:55
Description:
~ 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
}
User Comments: anonymous added on 2016-03-06 15:25:55:
I'm the reporter. Since this is not quite appropriate for the Tcl core, I'm closing this ticket.

dkf added on 2014-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


crshults added on 2014-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};

crshults added on 2014-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.