• Can this be done in one statement

    From Cecil Westerhof@21:1/5 to All on Wed Apr 12 13:38:27 2023
    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.

    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to Cecil Westerhof on Wed Apr 12 13:05:20 2023
    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)
  • From Cecil Westerhof@21:1/5 to Rich on Wed Apr 12 16:53:14 2023
    Rich <[email protected]d> writes:

    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.

    Yes, it is only the start of the loop. In the loop I use the epoch and
    command.


    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).

    As I said it is not a biggy. I think I keep it like it is. I find that
    more consistent and clearer. While it is almost impossible that the
    lists in history expand. I just find a list of lists clearer as a flat
    list. I wanted to be sure I was not overlooking something.
    Thanks.

    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to All on Wed Apr 12 15:28:45 2023
    From: Cecil Westerhof <[email protected]>
    Date: Wed Apr 12 14:53:14 GMT 2023
    Subject: Can this be done in one statement



    Cecil Westerhof <[email protected]> wrote:
    Sometimes I do something like:
    foreach cmdList ${history} {
    lassign ${cmdList} epoch command

    try:

    foreach {epoch command} [concat {*}$history] {


    Dave B

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cecil Westerhof@21:1/5 to [email protected] on Wed Apr 12 20:04:31 2023
    [email protected] writes:

    From: Cecil Westerhof <[email protected]>
    Date: Wed Apr 12 14:53:14 GMT 2023
    Subject: Can this be done in one statement



    Cecil Westerhof <[email protected]> wrote:
    Sometimes I do something like:
    foreach cmdList ${history} {
    lassign ${cmdList} epoch command

    try:

    foreach {epoch command} [concat {*}$history] {

    That works as long as history only contains lists of length 2.
    I let it simmer.

    --
    Cecil Westerhof
    Senior Software Engineer
    LinkedIn: http://www.linkedin.com/in/cecilwesterhof

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)