Re: Re[2]: Local variable acting static in 4GL report
Posted in 1997
Peter Neilson (peter_neilson@lendlease.com.au) wrote:
:
: Neil Gall wrote:
:
: >I seem to recall that I-4GL's report functions do not behave like true
: >functions in terms of privacy of locally declared variables. Perhaps >you
: could try a global tv variable instead, with something like:
: >
: >function report_driver()
: >...
: > foreach processed_row
: > ...
: > let tv = 0
: > output to report your_rpt(your_row.*) >...
: > end foreach
: > ...
: >end function
: >To my knowledge, global variables behave as they should in report
: >functions. Sorry I couldn't tell you why though.
: I have a feeling that global variables *may* not always do what you expect
: within report blocks. If the report is ordered internally, the values of
This is correct, and can be seen by a simple example:
MAIN
DEFINE rec RECORD LIKE table1.*
DEFINE glob_var CHAR(1)
DECLARE c1 CURSOR FOR
SELECT *
FROM table1START REPORT example
DISPLAY "Before foreach"
FOREACH c1 INTO rec.*
IF rec.col1 = "x" THEN
LET glob_var = "x"
ELSE
LET glob_var = "y"
END IF
DISPLAY "In foreach: ", glob_var
OUTPUT TO REPORT example (rec.*)
END FOREACH
DISPLAY "Post foreach"
FINISH REPORT example
END MAIN
REPORT example(rec)
DEFINE rec RECORD LIKE table1.*
#With this, doesn't work as expected: ORDER BY rec.col1
FORMAT
ON EVERY ROW
DISPLAY "In report: ", glob_var
...
END REPORT
---------------------------
Without the order by, you would get glob_var displayed directly after
the call to example() report, eg.
> Before foreach
> In Foreach: x
> In report: x
> In Foreach: y
> In report: y
> In Foreach: y
> In report: y
> In Foreach: x
> In report: x
> Post foreach
However, uncommenting the "order by" in the report will cause the
system to collect datasets sent to the report function until the
"FINISH REPORT" call, at which time it will order the data and do the
"ON EVERY ROW" in that order, with glob_var containing the last value,
eg.
> Before foreach
> In Foreach: x
> In Foreach: y
> In Foreach: y
> In Foreach: x
> Post foreach
> In report: x
> In report: x
> In report: x
> In report: x
This is non-intuitive behavior to most people who expect "OUTPUT TO
REPORT" to work exactly like a function call. Understanding that the
report can't process the data till after it orders it may help
conceptualizing what happens here.
Hope this helps,
Don Simon
ds5419@panix.com
Funk As Puck, Inc.
--
Don Simon
Funk As Puck, Inc.
ds5419@panix.com