List existing global variables, e.g. via varinfo()

The output of the function varinfo() is of type: Markdown.MD
For me it is not clear, how to handle this variable type.
My task is: Display the available variables
Below is my workaround, but I am sure, there is a more elaborated way to handle this output.
Here my snippet:

a = "foo"
varinfo_ = varinfo(; sortby=:name)
@info(string("\ntypeof varinfo()-output: ", typeof(varinfo_), " \n "))
_variable = Dict{String, String}()
for (i_name, _, i_summary) in varinfo_.content[1].rows[2:end]
    println("i_summary: ", i_summary, ", typeof(i_summary): ", typeof(i_summary))
    if i_summary != "Module"
        push!(_variable, (i_name => i_summary))
    end
end
@show _variable

varinfo just calls the names function and formats its output. It is probably easier to do what you want by calling names directly:

filter(n -> !isa(getfield(Main, n), Module), names(Main))

I submitted a PR to clarify the varinfo documentation: cross-reference names from varinfo docstring by stevengj · Pull Request #48779 · JuliaLang/julia · GitHub

1 Like