From: jrizzo on
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

From: Andrew Mangogna on
jrizzo(a)rizzos.net wrote:

> 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

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
From: blacksqr on
% regexp {([0-9]+): green} $output matchvar number
1
% puts $number
4


Andrew Mangogna wrote:
> jrizzo(a)rizzos.net wrote:
>
> > 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
>
> 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

From: Glenn Jackman on
At 2006-10-25 08:11PM, "jrizzo(a)rizzos.net" wrote:
> 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:


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

send -- "$green_choice\r"

--
Glenn Jackman
Ulterior Designer
From: jrizzo on
Thanks Much! This does exactly what I require.

Joe
Glenn Jackman wrote:
> At 2006-10-25 08:11PM, "jrizzo(a)rizzos.net" wrote:
> > 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:
>
>
> expect -re {(\d+): green.*Choose: *$} {
> set green_choice $expect_out(1,string)
> }
>
> send -- "$green_choice\r"
>
> --
> Glenn Jackman
> Ulterior Designer