Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

help selecting from a numbered list

2 views
Skip to first unread message

jri...@rizzos.net

unread,
Oct 25, 2006, 8:11:53 PM10/25/06
to
Hi-
I have a program I need to interact with which forces me to make a
choice based on a numbered list. I thought expect would be a good fit
for automating a task, however I am running into this issue - I don't
know how to get my expect script to choose based on the list returned
by the program.

Given the following output, how would I always select the number
corresponding to the text "green"?

-----------------
1: red
2: blue
3: yellow
4: green
5: purple
6: black
7: brown
8: grey
9: orange
10: pink
Choose:

Any help or direction is appreciated.

Thanks,
Joe

Andrew Mangogna

unread,
Oct 25, 2006, 10:55:18 PM10/25/06
to
jri...@rizzos.net wrote:

Assuming that you can get the "output" into a variable, the following
code will build a mapping of color names to numbers which can be used
find the number associated with "green":

==============================
set output {


-----------------
1: red
2: blue
3: yellow
4: green
5: purple
6: black
7: brown
8: grey
9: orange
10: pink
Choose:
}

foreach line [split $output \n] {
set elements [split $line :]
if {[llength $elements] == 2} {
set number [string trim [lindex $elements 0]]
if {[string is integer $number]} {
set color [string trim [lindex $elements 1]]
set colorMap($color) $number
}
}
}

parray colorMap
puts $colorMap(green)
==============================

The output of which is:

------------------------------
colorMap(black) = 6
colorMap(blue) = 2
colorMap(brown) = 7
colorMap(green) = 4
colorMap(grey) = 8
colorMap(orange) = 9
colorMap(pink) = 10
colorMap(purple) = 5
colorMap(red) = 1
colorMap(yellow) = 3
4
------------------------------
--
Andrew Mangogna

blacksqr

unread,
Oct 26, 2006, 12:07:29 AM10/26/06
to
% regexp {([0-9]+): green} $output matchvar number
1
% puts $number
4

Glenn Jackman

unread,
Oct 26, 2006, 9:37:37 AM10/26/06
to


expect -re {(\d+): green.*Choose: *$} {
set green_choice $expect_out(1,string)
}

send -- "$green_choice\r"

--
Glenn Jackman
Ulterior Designer

jri...@rizzos.net

unread,
Oct 26, 2006, 8:19:22 PM10/26/06
to
Thanks Much! This does exactly what I require.

Joe

0 new messages