How to execut a line of code in a char variable

cesaroll

Member
Is there a way in Progress to execute a line of code from a char variable or a string?

for example: in SQL Server:

exec ("select * from user")

or

exec (" a * b / 2")

or

StringVar = " a * b / 2"

exec (StringVar)

Thanks in advance!
 

Huushawdadi

New Member
That somewhat depends on where you're trying to execute the code to. I'm assuming that you don't mean a program execution which can be done using

run value(stringVar). "and expanded upon from there"

You can exec an OS command from a value(stringVar) arrangement. In fact for cross platform distribution this is the preferred method, ie. using the os-command function rather than unix, etc.

However, if you're trying to execute a database or numeric function within the Progress workspace then I don't believe so. (Anyone feel free to correct me)

In the past I "think" that I've tried to manipulate this situation before and I've come up with a few relatively simple ways of mimicing this but they both have drawbacks.

First up, I'm working on the assumption that you wish to use a stringVar for your execution statement due to conditional changes or to store the execution algorithm in a database store to be retrieved and executed conditionally without having to replicate the algorithm. They will relate differently also depending upon whether you are wishing to use this process in a single program or across programs.

All examples are very basic.

Method 1 - Preprocessed Include

The Include or Header file will contain either a series of preprocessors defined with your execution algorithms referenced by a unique preprocessor or if you need extra manipulations defined then use preprocessor operatives to get to the algorithm you need.

These can then be used in the following ways.
1a.
{./prealg.h}

intVal =
if cond1 then
{&preAlg1}
else
{&preAlg2}.

or
1b.

if cond1 then
intVal = {./prealg.i &preAlg1 = True}.
else
intVal = {./prealg.i &preAlg2 = True}.

There are more variations but thats the basics.

Pros
- keeps all algorithms set in the one source code accessible file location.
- simple to change algorithms
- simple to include in programs
- no database access required
- headers in the include can record when algorithms are changes and why
- can scope locally as desired using local vairables and values

Cons
- changing one algorithm will (most likely) require redelivery of all source code containing the include


Method 2 - Functions

Build a function or suite of functions that makes it's decisions based upon some input conditions and can cover any form of output datatype you want. Possbily build an include file using preprocessor definitions to scope these functions to each program.

Pros
- simple and easy to use
- can be inserted directly into calculations

Cons
- may not suit every purpose
- utilisation of an include file may cause the same impact analysis problems as Method 1.


Method 3 - Persistence and database

Define all your algorithms in a persistent program. Each to be accessed as a separate procedure call.
Store the procedure name in your database and retrieve that conditionally.
Execute using the value() syntax.

Pros
- Can define a procedure for each algorithm
- changes only require redelivery of the persistent program
- only a handle needs to be defined within the calling program
- only the database call and field assignment needs to be conditional

Cons
- no scoping for local values. Values must be passed into the persistence call
- for uniformity of execution input/output structure must be identical which will usually result in a temp-table being passed for input and or output.
- extra memory usage but that is scalable

Method 4 - Dynamic Functions

But I haven't had much experience in those..... :)

Hope it helps.

Cheers

Breck
 

Huushawdadi

New Member
mpowell,

Can you please expand that syntax? I've never seen that usage before and can't find it in the programming guide. Are you referring to RUN STORED-PROCEDURE statement or simple RUN statement?

I've only ever seen the run statement used with or without the value() command when referring to an internal/external or library procedure name.

How does it handle the execution of something like one of the original lines like "a * b / 2"?

I don't have Progress installed on this machine so can't test anything out but would appreciate seeing an example and what the result is.

Cheers,

Breck
 

JeremyGiberson

New Member
I'm currently trying to achieve the same thing. My current solution is to write my code to a temporary file then use run tempfile.p to execute it.

I'm still looking for a better solution if any one knows.

Thanks.
 

joey.jeremiah

ProgressTalk Moderator
Staff member
as the man said, it depends on what you're trying to execute.

if it's only math then sure. that's easy and i'll be happy to post code.

but no you cannot create and execute an entire program dynamically.
 
Top