Code Comments
Programming Forum and web based access to our favorite programming groups.Hi all, I am trying to create an expression builder dialog, which can handle logical, mathematical as well as relational operator queries. There is an entry widget where the user can enter the query/expression. Does anyone know of any easy way to do this, instead of parsing the string and doing push and pop stack procedure to calculate. The expression builder needs to handle +, -, *, /, <, >, <>, !, &&, ||, like, (, ). Any help is papreciated. Thanks in advance.
Post Follow-up to this messageMona wrote: > Hi all, > > I am trying to create an expression builder dialog, which can handle > logical, mathematical as well as relational operator queries. > > There is an entry widget where the user can enter the query/expression. > Does anyone know of any easy way to do this, instead of parsing the > string and doing push and pop stack procedure to calculate. > > The expression builder needs to handle +, -, *, /, <, >, <>, !, &&, ||, > like, (, ). How about : set result [expr $enteredExpression] -- +--------------------------------+---------------------------------------+ | Gerald W. Lester | "The man who fights for his ideals is | | Gerald.Lester@cox.net | the man who is alive." -- Cervantes | +--------------------------------+---------------------------------------+
Post Follow-up to this message"Gerald W. Lester" <Gerald.Lester@cox.net> wrote in message news:th13e.49364$Az.38522@lakeread02... > Mona wrote: > > How about : > set result [expr $enteredExpression] Seems Mona is looking for a parse tree rather than the answer. Looks like there's a good start in Tcl code on the wiki. http://mini.net/tcl/3498 > > -- > +--------------------------------+---------------------------------------+ > | Gerald W. Lester | "The man who fights for his ideals is | > | Gerald.Lester@cox.net | the man who is alive." -- Cervantes | > +--------------------------------+---------------------------------------+
Post Follow-up to this messageHi all,
Thanks for the reply.
I would like to compare a variable with the expression builder string
and perform some operations. My problem is how do I insert the
variable in the expression string?
So, the expresion need to be something like
[("my_variable" > "somevalue") && ("my_variable" < "othervalue")].
Something like a database query.
Mona.
Arjen Markus wrote:
> Roy Terry wrote:
handle
query/expression.
parsing the
&&, ||,
>
> Yet another one, which builds on the parsing technique used by [expr]
> itself
> is here: http://wiki.tcl.tk/13399
>
> Regards,
>
> Arjen
Post Follow-up to this message"Mona" <impm01@yahoo.com> writes:
> I would like to compare a variable with the expression builder string
> and perform some operations. My problem is how do I insert the
> variable in the expression string?
> [("my_variable" > "somevalue") && ("my_variable" < "othervalue")].
If I read you right, $my_variable is an expression...
Instead of:
if { ($my_variable > $somevalue) && ($my_variable < $othervalue) } {...
use:
if " ($my_variable > $somevalue) && ($my_variable < $othervalue) " {...
That will pre-expand the variables before expr looks at them.
Or were you talking about string comparisons?
--
Donald Arseneau asnd@triumf.ca
Post Follow-up to this messageMona wrote:
>
> Hi all,
>
> Thanks for the reply.
>
> I would like to compare a variable with the expression builder string
> and perform some operations. My problem is how do I insert the
> variable in the expression string?
> So, the expresion need to be something like
> [("my_variable" > "somevalue") && ("my_variable" < "othervalue")].
>
> Something like a database query.
>
If I understand you correctly, then you want to add a $ to every
variable name?
Well, the simplest way would be:
1. Maintain a list of variable names (or assume anything text that is
not quoted is a variable name)
2. Exploit [regexp] or [string map] to convert the raw but
non-Tcl-programmer-
friendly string into a proper Tcl [expr] expression.
If you want an example that may be beyond your needs, then have a look
at:
http://wiki.tcl.tk/11379 or http://wiki.tcl.tk/2822.
Otherwise:
set raw_expr {v > "a" && v < "m"}
regsub -all {([^"]|^)([a-zA-Z]+)} $raw_expr {$\2} converted_expr
set v "b"
expr $converted_expr
(Note: the regular expression is too simple - it fails with " m " for
instance :()
Regards,
Arjen
Post Follow-up to this messageThank you all for your replies. The wiki tcl pages were a great help. Thanks again. Mona.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.