user2883071
user2883071

Reputation: 980

How to store a line from expect_out(buffer) to a variable in tcl

For example, if the output ( in expect_out(buffer) )is

blah 
blh blah
asdjsudfsdf

how can I store the 2nd line to a variable? so far I have this:

foreach line [split $expect_out(buffer) "\n"] {
  if [lindex $line 1] {
    set variable $line
  }
}

But this does not work, it says the variable variable is undefined. I tried adding a counter, but that didn't work either. There has to be an easier way!

Upvotes: 0

Views: 4556

Answers (1)

glenn jackman
glenn jackman

Reputation: 247220

yes there is an easier way:

set lines [split $expect_out(buffer) \n]
set variable [lindex $lines 1]

or in one line

set variable [lindex [split $expect_out(buffer) \n] 1]

Keep mindful you know what Tcl commands return: split returns a list. You then use lindex to find the 2nd element of the list.

Upvotes: 4

Related Questions