How can I compress repetitive expect matches with a loop?

17 Views Asked by At

Suppose I have an array of patterns:

set pat(0) "foo"
set pat(1) "bar"
...

set loc(foo) "123"
set loc(bar) "456"
...

expect {
 -re "it's a $pat(0) in location (X)" {
    set $substr $expect_out(1, string)
    if { [string compare $loc($pat(0)) $substr] != 0 } {
      $loc($pat(0)) = $substr
      update_location
    }
  }
  -re # everything is the same for i = 1 .. 4 in my case
}

It's a pretty abstract function, but it is also very repetitive. I could (ideally) loop through the expression and annotate the expect statement with a commented out 'hardcode' version for readability.

But my understanding is any kind of loop in the expect block will break it.

Is there a good way to compress the code w/ expect?

0

There are 0 best solutions below