Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a command stored in string

Status
Not open for further replies.

MillTechNet

Technical User
May 28, 2009
72
0
0
GB
I'm having a mental blank today so can someone help me with this please?

I have a database field that contains a string. It is of the format >2 or <=4 You get the idea I'm sure. It always has a comparison operator and a number.

Now, what I need to do is a simple "if" statement to check a variable against this content. So if the field contained "<=4" then the if statement should work out to be

if VariableX<=4 then

but I cannot figure out how to do this, since the "<=4" bit is in the database field and can obviously change.

Any ideas?

 
One way would be to parse the operator and operand and then you could use a function to do the comparison. If you are using integers then it might look like:

Code:
function MyCompare(aVarable: integer; aOperator: string; aOperand: integer): boolean;
begin
  if aOperator = '>' then result := aVariable > aOperand;
  if aOperator = '>=' then result := aVariable >= aOperand;
  //and so on
end;

There is probably an more elgeant way to do this but I don't have it.
 
Thanks for that, I had considered that method but was wondering if there was a way to simply add the variable to the data field's string and then somehow execute the contents of the string. It seems a shame to have to parse the field to get the two bits out of it when most of the expression is there already.

It may be the only way to go though unless soeone else has a bright idea.
 
Parsing formulas is a college/university level exercise. You can get expression parsers, however. I think that the JCL has one.
 
I would write a routine to :
1) append the expression to a SQL string
2) pass the string to SQL query
3) process the query result

Roo
Delphi Rules!
 
Thanks roo0047, that inspired me to solve the problem.

Got it working perfectly now.

Many Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top