Fetch vs Run in Natural

Hi,

I’ve been investigating several programs and saw that a few of them are executing RUN commands. This was the first time I saw a code with a Run command in Natural. From what I’ve seen, run seems similar to Fetch except it compiles the code first before executing the object.

I was just wondering when do people use run over fetch and which one is more efficient.

Thanks in advance for any response.

Efficiency first. FETCH is more efficient than RUN. As you correctly stated, RUN requires a compilation, then an execution of the resultant object code. Since FETCH starts with the object code, no compilation is required.

So, why RUN? Simple. There is a facility in Natural I call “ampersand variables”, although technically, they are not variables at all.

Consider the following program (called report1)

FIND &FILE WITH &WITH
DISPLAY &DISPLAY
LOOP
END

Note the three “ampersand variables”.

The above program cannnot be STOW’ed.

Suppose I now have a “driver program” as shown below:

INPUT 'enter file name==> ’ +FILE (a32) // ‘enter serach criteria==>’ +WITH (a50) // ‘enter display fields’
+display (a50)
RUN ‘REPORT1’

I enter in EMPLOYEES for +FILE NAME = ‘SMITH’ for +WITH and CITY COUNTRY for +DISPLAY

Now we RUN report1. At compile time, when Natural sees an ampersand variable, it looks for a global variable with the same name. It then does a text replacement.

Hence, the program to be compiled is

FIND EMPLOYEES WITH NAME = ‘SMITH’
DISPLAY CITY COUNTRY
LOOP
END

At the end of the program report1 I could FETCH the driver program, enter in new report info, then RUN report1 again, generating a different report, etc.

steve

1 Like

Thanks so much for this Steve. This helped a lot! :smiley:

Hey Steve,

I know this is a old post, but thought of asking a follow up question.

I am using a program to call another sub program ( passing data using ambersant variables)

After the called program completes, I now want to run it from the next step of main program ( Like the fetch back).

So is it possible with RUN? I checked the documentation and didn’t find anything.

Regards,
Vasanth

First things first.

You are correct, there is no RUN RETURN statement as a counterpart to FETCH RETURN (or CALLNAT).

You can, however, write your own navigation logic using the Stack. Coding can get a little tricky, but usually not too terrible. Usually requires a “driver” program with a “dummy” REPEAT loop.

Thanks Steve,

I never used a STACK :frowning:

So I will need to read the basics first.

Meanwhile I am trying to find alternatives. Below is the reason why I had to use the & variables.

  1. I am using Define work file 1 ‘pathname’
    pathname will be - /home/MYID/MEMBER…kind of

  2. I want to assign the Member dynamically. I mean, the member will be decided based on the User input.

  3. I tried giving it as DEFINE WORK FILE 3 ‘/opt/sop/inputlib/’ #INPUT-MEM , which doesn’t seem to working.

So do you think there is a way out, so that I need not do the Stack stuff?

Regards,
Vasanth

DEFINE DATA LOCAL
1 #WORK (A35)
1 #INPUT-MEM (A10) INIT <‘USER-NAME’>
END-DEFINE
COMPRESS ‘/opt/sop/inputlib/’ #INPUT-MEM INTO #WORK LEAVING NO SPACE
**DEFINE WORK FILE 3 ‘/opt/sop/inputlib/’ #INPUT-MEM
DEFINE WORK FILE 3 #WORK
END

The work file must either be an all explicit designation, OR, an all implicit designation (as in #WORK). You cannot “mix and match” (as you tried to do).

Thanks Steve,

You truly are The Rockstar of Natural :slight_smile:

This reduces a lot of my effort. I need not use & variables and the Stack now… :lol:

So Thanks a lot Steve Ji.

Regards,
Vasanth

Hi everybody,

After exaustively look for at internet, i tought i had found a solution to my problem using Run to pass parameters to my program. But not. I loose context using run with my original program.

What i’m trying to do is:
Execute diferent “Read” comands to read an adabas file but i would like to use several different descritor/super-descritor and diffenrent values to read this descritor. Than i tried use the Include command with parameter to change the descritor to solve my problem but the include incorporate the hole command at stow.

In the code below i use +campo and +valor global variables the change the fields to use as descritor and

Program copycode ‘test1’:
0080 FIND F168-VIEW WITH &CAMPO = &VALOR
0090 WRITE NOTITLE ‘=’ &CAMPO
0100 END-FIND

Program ‘test2’:

0730 MOVE ‘CO-UNIDADE-08’ TO +CAMPO
0740 MOVE ‘’‘1010500’‘’ TO +VALOR
0750
0800 INCLUDE TEST1

But it doesn’t work because Test2 includes fixed values for &campo and &valor

Are there any way to change at execution time values for &campo and &valor so at execution of Test2 performs different selections ???

It is easy on others languages called as “Indirection” but i couldn’t find similar command at Natural help.
The version of my Natural is 8.2.5.1

Thanks for any help

It is difficult to know where to start. So, lets start with something simple.

As you noted, you wanted to:

This is quite impossible using your code layout. As you yourself noted, there is no way to change &CAMPO and &VALOR except by changing the underlying global variables and recompiling (or re-Running, see below).

Depending on how many descriptor fields you might be using, one way to do what you want would be to have something like:

DECIDE various conditions or values
WHEN condition 1
PERFORM first combination of descriptor and value
WHEN condition 2
PERFORM second combination of descriptor and value

where each combination is a local (or stand alone) subroutine.

Actually, for the values (assuming all are alpha) you could use a local variable such as #VALOR. Then all the FINDs would be something like FIND … WITH CO-UNIDADE-08 = #VALUE.

The above is probably the easiest way to go and maintain.

If you want to use ampersand variables, you could “combine” test1 and test2 into a single source program called, just test.

Then you could have something like:

0080 FIND F168-VIEW WITH &CAMPO = &VALOR
0090 WRITE NOTITLE ‘=’ &CAMPO
0100 END-FIND

IF something true

MOVE ‘CO-UNIDADE-08’ TO +CAMPO
MOVE ‘’‘1010500’‘’ TO +VALOR
RUN ‘test’
else STOP

This is typically rather silly code, but it does work. NOT recommended.

The reason it is NOT recommended is that you would lose values of all the other local variables, since the program test would be recompiled. Yes, you could pass values between different executions of test, but this could be VERY messy depending on how may such variables there are.

Steve,

Thanks for your reply.
I could learn something i havent never used. Call the same program.
This is a good idea. I tested it and i could control the execution fluxo by a variable in a global area. Consecutive RUN calls to the same program using a global never lose the values. Thanks for this solution. :slight_smile:

But the problem now is that this program will be called by a micro procedure running in a low end asp server.
This low end asp calls the natural program and stay waiting for a response.

My solutions till today works fine because sequencial fetch return or callnat ou perform comands always go back to original first natural called program and the micro procedure always receive an answer from Natural High end computer.

If i use a run call passing parameters in this first called Natural program, it will run fine but my original called Natural program will never ends correctly.

I read something about the use of an stack to control the sequence of an Natural routines.

Do you think i could use stack solution to control this sequence on Natural environment and go back to my first called Natural program ?

If yes, how use stack ? I never used it before.

Thanks again.