On Sat, 12 Jul 2008, Allin Cottrell wrote:
[about the problems that can arise when a named list is passed
as an argument to a user-defined function]
Here's a proposal for solving the ... problem. It is
implemented in CVS but not by default, only if you edit
lib/src/gretl_func.c and change the definition
#define PROTECT_LISTS 0
to
#define PROTECT_LISTS 1
The essential component is that, when a series is passed to a
function as part of a list argument, the series is _not_
visible/accessible by name within the function...
The above account still holds in current CVS. But the
following...
Accordingly, we modify the action of a "foreach i" loop
over the
members of a list argument to a function. Instead of "$i"
retrieving the name of the series at position i in the list, it
retrieves the ID number of that series...
has been changed in light of Jack's comments. In the context
noted above, "$i" now gets the name of the series at position i in
the list, as usual. To get the _values_ of this series (for
example, in the context of genr), however, you cannot just use the
name as provided by "$i", you must give the name of the list as a
prefix, separated from the series name by a dot, on the pattern
listname.varname.
Here is a newly revised version of the cubes function that I used
for illustration previously.
function make_cubes (list xlist)
list retlist = null
loop foreach i xlist --quiet
series $i3 = xlist.$i^3
list retlist = retlist $i3
end loop
return list retlist
end function
Please note that The "varcopy" function mentioned in my posting of
12 July has now been removed from CVS.
Allin.