Hi,
I needed to check whether a bundle contains a certain gretl object type,
so I put together a fairly general but straightforward function that
counts all the various types.
Comments welcome; in particular whether this should go into the "extra"
addon. Also, do you like the return format or do you have a better idea?
thanks
sven
--
<hansl>
set verbose off
function matrix bundle_member_types (const bundle b)
# Checks the types of the members of b and counts their occurrences.
# Returns a vector with 12 rows, holding the contained number of
members
# of the possible types in the order corresponding to the inbundle()
# function:
#
# 1. scalar, 2. series, 3. matrix, 4. string, 5. bundle,
# 6. array, 7. list.
#
# Since an 'array' can be of various sub-types, the rows 8-12
# hold the numbers of those sub-types:
# 8. matrices, 9. bundles, 10. strings, 11. lists, 12. arrays.
#
# Therefore the sum of rows 8-12 equals the number in 6.
# init
matrix out = zeros(12)
rnameset(out,
defarray("scalar","series","matrix","string","bundle",
\
"array", \
"list", \
"matrices","bundles","strings","lists","arrays"))
# check all the members
if nelem(b)
keys = getkeys(b)
loop foreach i keys
type = inbundle(b, "$i") # should be non-zero by construction
out[type]++
if type == 6 # array
string arraytype = typename(b["$i"])
if arraytype == "matrices"
out[8]++
elif arraytype == "bundles"
out[9]++
elif arraytype == "strings"
out[10]++
elif arraytype == "lists"
out[11]++
elif arraytype == "arrays"
out[12]++
else
print arraytype
funcerr "shouldn't happen"
endif
endif
endloop
endif
# quick cross-check
errorif(out[6] != sum(out[8:12]), "array counts don't match")
return out
end function
# test case:
open denmark --quiet
var 2 LRM LRY IBO --silent
eval bundle_member_types($system)
eval $system
</hansl>