For Programmers: Free Programming Magazines  


Home > Archive > Tcl > October 2006 > help selecting from a numbered list









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author help selecting from a numbered list
jrizzo@rizzos.net

2006-10-30, 7:32 pm

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

2006-10-30, 7:32 pm

jrizzo@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
blacksqr

2006-10-30, 7:32 pm

% regexp {([0-9]+): green} $output matchvar number
1
% puts $number
4


Andrew Mangogna wrote:
> jrizzo@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


Glenn Jackman

2006-10-30, 7:32 pm

At 2006-10-25 08:11PM, "jrizzo@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
jrizzo@rizzos.net

2006-10-30, 7:32 pm

Thanks Much! This does exactly what I require.

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


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com