Re: expect: how to clear expect_out(1,string)
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: expect: how to clear expect_out(1,string)

From: Uwe Klein <uwe_klein_habertwedt@t-online.de>
Date: Mon Mar 06 2006 - 19:15:17 CET

andyhaupt@netscape.net wrote:
> Hello,
>
> I'm using the following pattern in an expect script
>
> expect -re "(pat1)?(pat2)"
>
> ie. pat1 can match 0 or 1 time, pat2 needs to match always.
>
> the output is stored in expect_out(1,string) and expect_out(2,string)
> but in the case that pat1 does not match expect_out(1,string) is still
> filled with the output of the last successful match.
> This might sound strange but is also documented in 'Exploring Expect'
> on page 112 in my copy.
>
> Now if I base a decision on the value of expect_out(1,string) I might
> be mislead since it does not refer to the current match so I'd rather
> have an empty string.
> Is there a way to clear expect(1,string)?
> What is the best way to base decisions on whether pat1 has been seen?
>
> I was thinking about a hierarchical approach like this
> expect {
> -re "(pat1)(pat2)" { # pat1 and pat2 have been seen }
> -re "(pat2)" { # only pat2 has been seen }
> }
> is this really the way to do it?
>
> rgds
> andy
>
well, you should have the

complete pattern in expect_out(0,string)
pat1 in expect_out(1,string)
pat2 in expect_out(2,string)

if pat1 was not seen expect_out(1,string) does not exist

right?

a little test:
expect_user {
     -re "(pat1)*?(pat2)" { # pat1 and pat2 have been seen
                         puts HIT:pat12
                         foreach idx {0 1 2 3} {
                                 catch {set expect_out($idx,string)} cerr
                                 puts $idx:$cerr
                         }
}
     -re "(pat2)" { # only pat2 has been seen
                         puts HIT:pat1 }
}

test:
input: "pat2\r"
HIT:pat12
0:pat2
1:can't read "expect_out(1,string)": no such element in array
2:pat2
3:can't read "expect_out(3,string)": no such element in array

iput: "pat1pat2\r"
pat1pat2
HIT:pat12
0:pat1pat2
1:pat1
2:pat2
3:can't read "expect_out(3,string)": no such element in array

input: "pat1pat1pat1pat2\r"
HIT:pat12
0:pat1pat1pat1pat2
1:pat1
2:pat2
3:can't read "expect_out(3,string)": no such element in array

alternate:

set pat1cnt 0
expect {
     -re "(pat1)" { # pat1 has been seen
                        incr pat1cnt
                }
     -re "(pat2)" { # only pat2 has been seen
                        if {pat1cnt} {
                                do blabla
                        }
                        set pat1cnt 0
                }
}

uwe
Received on Sun Apr 30 02:25:02 2006