There's a function I wanted in the context of svm() -- to get all
the
keys from a bundle in the form of an array of strings -- and I thought
it might be useful to have it exposed in hansl under the name getkeys.
Could be helpful for function writers, if you're taking in a bunch of
arguments in the form of bundle elements, some of them optional.
The idea is that you can check for mistyping of parameter names, and
hence maybe save a user some puzzlement ("I set X but it didn't have
any effect!").
Trivial illustration follows.
<hansl>
function scalar in_strings_array (const strings S, const string s)
scalar ok = 0
loop i=1..nelem(S) -q
if s == S[i]
ok = 1
break
endif
endloop
return ok
end function
function void do_something (const bundle parms)
strings ok_keys = defarray("foo", "bar", "baz")
strings pnames = getkeys(parms)
loop i=1..nelem(pnames) -q
if !in_strings_array(ok_keys, pnames[i])
errstr = sprintf("Unrecognized parameter '%s'", pnames[i])
funcerr errstr
endif
endloop
end function
bundle b = defbundle("foo", 3, "bar", "some text",
"bax", I(2))
do_something(b)
</hansl>
Here, do_something() accepts a bundled parameter "baz" (implicitly
optional) which has been mistyped as "bax". Instead of "bax" being
silently ignored the result is:
Error message from do_something():
Unrecognized parameter 'bax'
I guess the example invites the idea that inarray() could be a
built-in function for arrays of strings (at least). Any thoughts on
whether that would be worthwhile?
Allin
_______________________________________________
Gretl-devel mailing list
Gretl-devel(a)lists.wfu.edu
http://lists.wfu.edu/mailman/listinfo/gretl-devel