Cecil Westerhof <
[email protected]> wrote:
Sometimes I do something like:
foreach cmdList ${history} {
lassign ${cmdList} epoch command
Can this be done in one statement?
Not a biggy, more a nice to have.
If you just want to 'expand' the contents of history into separate
variablles, look at 'lassign' much as you have above.
If you want to grab the last element off of the history list and expand
it, the a combination of lassign and lindex would do so (your snippet,
if taken literally, would iterate the list only to find the last
element therein):
lassign [lindex $history] epoch command
If you actually want to process each 'cmdList' within history (i.e.,
there is more happening after the lassign) then so long as history is a
list of lists you need to perform the above operation.
However, if you can change history to be a flat list, not a nested
list:
lappend history $epoch $command
...
lappend history $epoch $command
...
lappend history $epoch $command
Then you can iterate all the epoch/command pairs like so:
foreach {epoch command} $history {
# do things with epoch and command
}
Do note that the nested list version can be extended by appending
additional list elements to the nested list, without the foreach loop
requiring change. Including some nested lists only having two elements
while others have three or more.
The flat list variant requires every "entry" be the identical number of elements, and adding an element requires adding a loop variable to
every foreach that walks the list, even if the loop variable is a
"throwaway variable". So it requires much more code change to extend
(if you have more than one location that walks over the history list).
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)