Skip to content
Tcl Source Code View Ticket
Login

View Ticket

"lgroup" to regroup a list

Closed0d2bcd9544735adea2d2c3aabd5f0d1e222cb4fcCreated 13 years agoUpdated 11 years ago
Descriptionplain2014-01-12 00:21:31
~ Prototype

lgroup listIn lengthOfSublist ?stepForward?

stepForward defaults to $lengthOfSublist

~ Example

set l {a b c d e f g h}
lgroup $l 4 # {a b c d} {e f g h}
lgroup $l 3 # {a b c} {d e f} {g h}
lgroup $l 1 # a b c d e f g h
lgroup $l 0 # {a b c d e f g h}
lgroup $l -1 # error
lgroup $l 3 2 # {a b c} {c d e} {e f g} {g h}
lgroup $l 2 3 # {a b} {d e} {g h}

~ Usefulness

This can be used to rearrange results returned by SQLite3.

~ Incomplete implementation

Since I just use it to process what SQLite3 returns, I have ignored the "stepForward". I think rewrite it in C will speed it up. I didn't check whether $lengthOfSublist is a negative number or zero.

proc lgroup {listIn lengthOfSublist} {
	set i 0
	foreach it $listIn {
		lappend tmp $it
		if {[llength $tmp] == $lengthOfSublist} {
			lappend result $tmp
			set tmp {}
		}
		incr i
	}
	if {[llength $listIn] % $lengthOfSublist} {
		lappend result $tmp
	}
	return $result
}
anonymous Ticket [0d2bcd9544]: 3 changes [History diff]2014-01-12 00:42:44
anonymous Ticket [0d2bcd9544]: 3 changes [History diff]2014-01-12 00:58:21
dkfhtml2014-01-12 21:56:37

Don't worry about getting performance or the C implementation. Worry about why and what (i.e., a clear justification for this command and an exact specification of the behaviour).

dkf Ticket [0d2bcd9544]: 3 changes [History diff]2014-01-13 19:40:17
anonymousplain2016-03-06 15:25:17
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.