Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

expect script question

Status
Not open for further replies.

abm086

Technical User
Jun 14, 2002
14
US
Hello,
I am wondering how I can get a variable to contain more than one value.(like a korn shell array) Then how can I get the script to run a for loop on each entry in the variable.

thanks in advance
 
You can use tcl lists or the tcl array.

Try this example.

#!/usr/bin/expect
#start
array set myhosts {
localhost 127.0.0.1
localbroadcast 127.255.255.255
}

set myhostslists [list 127.0.0.1 127.255.255.255]

#The foreach loop is commonly used.
foreach y [array name myhosts] x $myhostslist {
catch {puts "ARRAY: $myhosts($y)"}
catch {puts "LIST : $x"}
}

#end

You can use the familiar C for loop as well with
a list easily:

for {set x 0} {$x <= [llength $myhostslists]} {incr x} {
puts [lindex $myhostslist $x]
}

Or by using numeric indices for the tcl array:

array set myhosts {
1 127.0.0.1
2 127.255.255.255
}

for {set x 1 } {$x < [array size myhosts]} {incr x} {
puts &quot;$x = $myhosts($x)&quot;
}

Another cool thing you can do with tcl lists and arrays is search for keys or patterns in indices, and lists using
the [array name &quot;globpatternhere&quot;], and lsearch commands.
Remember that expect is a superset of tcl and is based
on tcl and uses all of the control and data constructs
in tcl. Getting to know tcl is the best way to use expect.
A better forum for your questions is the tcl group here at Tek-Tips.

HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top