rtl::seteach

wrapper around foreach … break sequences.

seteach {a b} [list 1 2]
# replaces: foreach {a b} [list 1 2] breakset a  ➠ 1
set b  ➠ 2

Seteach does also support nested lists.

Example 9.6. Seteach with nested Lists

seteach {{{a b} c} {d e} f} \
  [list [list [list 1 2] 3] [list 4 5]]

  …
  a ➟1, b ➟2, c ➟3, d ➟4, e ➟5, f nothing.

Seteach always expects the same amount of values and variables –for security reasons. That is seteach {a b} 1; produces a variable »a b« with the value »1«. And seteach {a b c} {1 2} produces a variable »a b c« with the value »1 2«.

The nested list extension over the simple foreach … break was written to support tkpath::mmult.

On foreach .. break: break alters the control flux inside of an application. Using such a keyword in a conventional expression, makes it a commonplace figure inside the source code; obscuring where it alters the flux. This is highly dangerous and therefore »seteach« exist.

Seteach accepts an optional argument »format«. The value for -format is a format string.

Example 9.7. seteach with format

  seteach {x y} {x y} -format {winfo %s .}
  ➟ x := window x position of ».«
    y := window y position of ».«
        

Format can be used during navigation. The format string must return exact the same atoms as expected by the variable definition.

Example 9.8. Forbidden format usage

  set mylist {{1 2} x y z {3 4 f o r b i d d e n}}
  seteach {
     {x y} {v w}
   } {0 end} -format {lindex $mylist %s}

    ➟ {v w} := {3 4 f o r b i d d e n} ;# error
         x  := 1
         y  := 2 

  # The end item has 11 atoms and
  # only two are expected.
        

Navigation through encapsulated lists requires, that the amount of atoms inside the sublist is identical to the number of expected replacements.