Focal Point
[CLOSED] Setting a variable from calling an external fex

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/2987017726

January 07, 2013, 04:47 PM
Daniel G
[CLOSED] Setting a variable from calling an external fex
Is there a way to set a variable in a define by calling an external fex file?

Example:
DEFINE FILE MY_COUNTS
COUNT_1/I8 = EX some_fex_file.fex
END

Is this possible or is there a better way of doing this?

Thank,

Daniel

This message has been edited. Last edited by: Kerry,


In Focus since 2012
WebFOCUS 8.0.07
Windows, All Outputs
January 07, 2013, 05:05 PM
Waz
With thi sort of thing you would call the fex and use one of its results.

In this example the some_fex_file will set an amper variable to a value.

-INCLUDE some_fex_file
DEFINE FILE MY_COUNTS
COUNT_1/I8 = &Res
END


You can also read a hold file from an EX or set a global Amper variable.


Waz...

Prod:WebFOCUS 7.6.10/8.1.04Upgrade:WebFOCUS 8.2.07OS:LinuxOutputs:HTML, PDF, Excel, PPT
In Focus since 1984
Pity the lost knowledge of an old programmer!

January 08, 2013, 09:25 AM
jgelona
Depending on what you are doing, DEFINE FUNCTION may be an option.


In FOCUS since 1985. Prod WF 8.0.08 (z90/Suse Linux) DB (Oracle 11g), Self Serv, Report Caster, WebServer Intel/Linux.
January 08, 2013, 09:31 AM
benicely
Waz, in your example

COUNT_1/I8 = &Res

Does &Res get set in the some_fex_file?


WebFOCUS 7.6
Windows, All Outputs
January 08, 2013, 09:50 AM
benicely
jgelona,

I'm Daniel's co-worker, I'll try to describe what we're doing.

We are getting data based on multiple parameters. For example: Count all of the yellow items that weigh less than 1 lb. that were in stock in the month of Jan, Feb, ..., Dec.

Using a loop, we're able to get the data we want, but it runs 12 different reports.

The code looks something like this:

report_display.fex
Loop through each month Jan, Feb, ..., Dec

TABLE FILE ITEM_DATA
-INCLUDE count_items.fex
ON TABLE HOLD AS ITEM_DATA
END

count_items.fex
...Work to get what we want...
TABLE FILE ITEM_COUNT
SUM 
   CNT.ITEMS
ON TABLE HOLD AS ITEM_COUNT
END


When we run the code, we get 12 different tables with the correct counts, which we're expecting. But we'd like to be able to just store these numbers and pass them back to the parent so they can be displayed along with other fex's we'll need to call to get other data.

The reason we don't just put this in one file is because we'll eventually need to do this with all the blue, red, green items. We're trying to have as little duplicate code as possible.

When we call the count_items.fex file, we'll pass in what the variables needed to retrieve the count from the parent.

Any help on how to do this would be greatly appreciated.

Thanks,
Brian


WebFOCUS 7.6
Windows, All Outputs
January 08, 2013, 10:43 AM
Mighty Max
You can build your list of DEFINE fields and PRINT fields.
Then use them in your table request.
Take a look at sample below.
  
-* Create the list of Define fields
TABLE FILE CAR
SUM
   COMPUTE CNTRY_LEN/I8 = ARGLEN(10, COUNTRY, CNTRY_LEN); NOPRINT
   COMPUTE SEAT_ALPHA/A11 = EDIT(CNT.SEATS); NOPRINT
   COMPUTE SEAT_CNT/A11 = IF CNT.SEATS EQ 0 THEN '0' ELSE TRIM('B', SEAT_ALPHA, 11, '0', 1, 'A11'); NOPRINT
   COMPUTE DEFI_FLD/A500 = CTRAN(CNTRY_LEN, COUNTRY, 32, 95, 'A10') || '_CNT' || '/I11 = ' | SEAT_CNT || ';' ;
BY COUNTRY NOPRINT
ON TABLE SET ASNAMES ON
ON TABLE SET HOLDLIST PRINTONLY
ON TABLE HOLD AS DEFI_FLD
END
-RUN

-* Create the list of Print fields
TABLE FILE CAR
SUM
   COMPUTE CNTRY_LEN/I8 = ARGLEN(10, COUNTRY, CNTRY_LEN); NOPRINT
   COMPUTE PRNT_FLD/A500 = CTRAN(CNTRY_LEN, COUNTRY, 32, 95, 'A10') || '_CNT' | ' AS ' | '''' | LCWORD2(10, COUNTRY, 'A10') || ',' || 'Count' || '''';
BY COUNTRY NOPRINT
ON TABLE SET ASNAMES ON
ON TABLE SET HOLDLIST PRINTONLY
ON TABLE HOLD AS PRNT_FLD
END
-RUN

DEFINE FILE CAR
-INCLUDE DEFI_FLD
END

TABLE FILE CAR
PRINT
-INCLUDE PRNT_FLD
BY COUNTRY NOPRINT
WHERE RECORDLIMIT EQ 1;
END
-RUN



WebFOCUS 8.1.05M Unix Self-Service/MRE/Report Caster - Outputs Excel, PDF, HTML, Flat Files
January 08, 2013, 11:13 AM
benicely
Mighty Max, what if you wanted to split your code up into different fex files, or is this not a good idea?


WebFOCUS 7.6
Windows, All Outputs
January 08, 2013, 02:24 PM
Waz
From my understanding of your first example, you wanted to have a fex return a value.

You can set an amper variable in the called fex and use it in the calling fex.

-INCLUDE adds a fex inline, and EX executes a FEX with a clean environment.

There are many examples of creating an amper variable from some data in the forum.


Waz...

Prod:WebFOCUS 7.6.10/8.1.04Upgrade:WebFOCUS 8.2.07OS:LinuxOutputs:HTML, PDF, Excel, PPT
In Focus since 1984
Pity the lost knowledge of an old programmer!

January 08, 2013, 02:40 PM
John_Edwards
Here's an example of code I use to get date values put into amper variables for later usage:

 
TABLE FILE CAR
PRINT COUNTRY NOPRINT
      COMPUTE CURRENT_DATE/A8 = EDIT(TODAY('A10'), '$$$$$$9999') | EDIT(TODAY('A10'), '99$99');
      COMPUTE CURRENT_DATE_YYMD/YYMD = DATECVT(CURRENT_DATE, 'A8YYMD', 'YYMD');
      COMPUTE CUTOFF_DATE/YYMD = DATEADD(DATEMOV(DATEADD(CURRENT_DATE_YYMD, 'M', -1), 'BOM'), 'D', -1);
WHERE RECORDLIMIT EQ 1;
ON TABLE SET HOLDLIST PRINTONLY
ON TABLE HOLD AS DATEVALUES FORMAT ALPHA
END
-RUN

-READ DATEVALUES &CURRENT_DATE.A8 &CURRENT_DATE_YYMD.A8 &CUTOFF_DATE.A8
-TYPE &CURRENT_DATE


You could do the same thing where your TABLE FILE command produces your sum and drops it into a hold file, then the -READ pulls the values from the hold and puts them into ampervariables. Once they're in ampers you can do anything you like with them, including assigning them to DEFINE fields later in your code.

Notice that the -READ is reading from the file created by the TABLE FILE command.

Note -- HOLDLIST PRINTONLY very important, FORMAT ALPHA very important. Make sure you only have one line of output so the -READ grabs that line.

I'll be honest, it looks like you're dividing your code into a ton of pieces to "avoid duplication" which is a worthy goal. But maintainability should be your primary goal since it's the most expensive part of your process. Don't slice the pie into too many pieces.

J.



January 08, 2013, 03:21 PM
Mighty Max
I don't see a problem with breaking it into 3 fexes. Why don't you go ahead and test it out.
  
-* Create the list of Define fields
-INCLUDE build_defines.fex

-* Create the list of Print fields
-INCLUDE build_prints.fex

DEFINE FILE CAR
-INCLUDE DEFI_FLD
END

TABLE FILE CAR
PRINT
-INCLUDE PRNT_FLD
BY COUNTRY NOPRINT
WHERE RECORDLIMIT EQ 1;
END
-RUN


Also as John pointed out trying to reduce the number of files and increasing the amount of reusable code sounds cool but it is a PIA to maintain.
I was on project where everything was dynamic. The table name, by fields, print fields, where clauses (multiselect) and much more. And then we had to provide drilldowns as well. It was fun but not for the average WebFocus Developer.


WebFOCUS 8.1.05M Unix Self-Service/MRE/Report Caster - Outputs Excel, PDF, HTML, Flat Files
January 10, 2013, 03:43 PM
Daniel G
Mighty Max, can you explain more what

DEFINE FILE CAR
-INCLUDE DEFI_FLD
END

does? I am new to Webfocus and am trying to understand your code. Thanks

Daniel


In Focus since 2012
WebFOCUS 8.0.07
Windows, All Outputs
January 10, 2013, 03:57 PM
Waz
-INCLUDE add WebFOCUS code from another fex inline with this one, as if it is part of the calling fex.

For example

DEFI_FLD.fex contains
COUNT_1/I9 = {some code} ;


Your fex will at execution effectively contain
DEFINE FILE CAR
COUNT_1/I9 = {some code} ;
END


I would suggest that you look into getting some training on WebFOCUS.


Waz...

Prod:WebFOCUS 7.6.10/8.1.04Upgrade:WebFOCUS 8.2.07OS:LinuxOutputs:HTML, PDF, Excel, PPT
In Focus since 1984
Pity the lost knowledge of an old programmer!