Home > Archive > Tcl > July 2007 > Question about the 'expr' command in tcl
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 |
Question about the 'expr' command in tcl
|
|
|
| Dear friends:
I wrote a tcl script which has two arguments. But when I try
set a [lindex $argv 0] ;# this command seems find
set b [expr {0.06 * $a +1}] # this command encountered the following
error
'can't use empty string as operand of "*" '
when I run this script I already give the value for the argument so I
think the string 'a' should already been initialized before the the
expr.
could anybody help me with this. Any advive will be very appriciated.
Howie
| |
| tclcoder@nurdglaw.demon.co.uk 2007-07-24, 8:10 am |
| Howie,
On 24 Jul, 12:56, allke <allke0...@gmail.com> wrote:
> Dear friends:
>
> I wrote a tcl script which has two arguments. But when I try
>
> set a [lindex $argv 0] ;# this command seems find
> set b [expr {0.06 * $a +1}] # this command encountered the following
> error
>
> 'can't use empty string as operand of "*" '
>
> when I run this script I already give the value for the argument so I
> think the string 'a' should already been initialized before the the
> expr.
>
> could anybody help me with this. Any advive will be very appriciated.
Three thoughts:
i) It looks very much as though a ISN'T being set up by your first
line. Have you tried printing it out, e.g. with
puts stderr *$a*
to check?
ii) How are you running your script? argv gets set up by the tcl shell
shell so if you do
shell-prompt$ tclsh <your script> 95
it should get set up. If you're doing
tclsh> <your script> 95
it won't.
iii) Are you doing anything in your script before the lines you quote
that might affect things?
Hope this helps,
Alan
| |
| Larry W. Virden 2007-07-24, 8:10 am |
| On Jul 24, 7:56 am, allke <allke0...@gmail.com> wrote:
> I wrote a tcl script which has two arguments.
How do you run it?
In cases like this, my typical debugging session puts the smallest
number of lines possible into a file and run them, to see if I have
the syntax, etc.
right.
So, in this case, I might try:
$ cat > tstarg.tcl
#! /path/to/tclsh
puts "number of args: $argc"
set index 0
foreach arg $argv {
puts "argument $index [lindex $argv $index]"
incr index
}
^D
$ tclsh tstarg.tcl 1 2 3 x y z
number of args: 6
argument 0 1
argument 1 2
argument 2 3
argument 3 x
argument 4 y
argument 5 z
$
If you don't get this, then something went wrong in your syntax.
If you do get this, then try
$ ./tstarg.tcl 1 2 3 x y z
number of args: 6
argument 0 1
argument 1 2
argument 2 3
argument 3 x
argument 4 y
argument 5 z
You should get exactly the same thing. If you don't, then something's
wrong with that first line.
Oh, make certain that when you run your program, you actually are
running the version you think you are. Sometimes, if you don't use ./
pgmname or /path/to/pgmname , your shell might find an executable with
the same pgmname in another directory, and so your edits aren't being
tested.
| |
| Darren New 2007-07-24, 7:13 pm |
| allke wrote:
> Dear friends:
>
> I wrote a tcl script which has two arguments. But when I try
>
> set a [lindex $argv 0] ;# this command seems find
This might set 'a' to the empty string, if argv doesn't have any
elements. [lindex "" 0]==""
> set b [expr {0.06 * $a +1}] # this command encountered the following
> error
>
> 'can't use empty string as operand of "*" '
Here, expr tells you $a is empty.
Try putting
puts "($argv)"
as the first line and see what you get.
--
Darren New / San Diego, CA, USA (PST)
I bet exercise equipment would be a lot more
expensive if we had evolved from starfish.
|
|
|
|
|