tags:

views:

51

answers:

1

i wrote a script for spawing the bc command

package require Expect

proc bc {eq} {
    spawn e:/GnuWin32/bc/bin/bc
    send "$eq\r"
    expect -re "(.*)\r"
    return "$expect_out(0,string)"
}

set foo "9487294387234/sqrt(394872394879847293847)"

puts "the valule [bc $foo]"

how to get the output from this. When i am running this one i get ouput like this

bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
9487294387234/sqrt(394872394879847293847)
477
can't read "expect_out(0,string)": no such element in array
    while executing
"return "The values is $expect_out(0,string)""
    (procedure "bc" line 6)
    invoked from within
"bc $foo"
    invoked from within
"puts "the valule [bc $foo]""
    (file "bc.tcl" line 21)

how to resolve this one.

A: 

The problem is that it's not matching what you expect. (Cue the Jamie Zawinski quote.)

Try this:

package require Expect
proc bc eq {
    spawn bc; # It's on my path...
    send $eq\r
    set timeout 1; # 1 second
    expect timeout {} -re {[^\r\n]+} {
        set lastline $expect_out(0,string)
        exp_continue
    }
    close
    return $lastline
}
puts >>[bc 9487294387234/sqrt(394872394879847293847)]<<

I get this output...

spawn bc
9487294387234/sqrt(394872394879847293847)
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
9487294387234/sqrt(394872394879847293847)
477
>>477<<
Donal Fellows
but it didn't works
Prime
Part of the problem is that `bc` doesn't have any prompts.
Donal Fellows
@Mallikarjunarao: Try that update of the answer. It works for me...
Donal Fellows
I get output like thisD:\TCLLab>tclsh86 bc.tclbc 1.06Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.This is free software with ABSOLUTELY NO WARRANTY.For details type `warranty'.9487294387234/sqrt(394872394879847293847)4777
Prime
@Mallikar: Well I don't know what's wrong. I try with *exactly* the code from above (i.e., cut-n-paste it into a file) and it works for me exactly as described.
Donal Fellows
@Donai i am not getting total output of the resultbc 1.06Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.This is free software with ABSOLUTELY NO WARRANTY.For details type `warranty'.9487294387234/sqrt(394872394879847293847)477>>7<<
Prime
I guess that might be an issue with the Windows version of expect. The stupid thing is that you can drive `bc` without using expect at all: `set f [open |bc r+];fconfigure $f -buffering none;puts $f 9487294387234/sqrt(394872394879847293847);gets $f result;close $f;puts $result`
Donal Fellows