Get a list of current variables in Julia Lang

Julia

Julia Problem Overview


I am new to Julia Lang. I am coming from the background of Matlab.

In Matlab, when pressing whos command I will get all variables in the current scope; and also, I can store them in another variable like x=whos; Is there such commands exists in Julia? Example code in Matlab:

>> a=3;
>> b=4;
>> whos
Variables in the current scope:

Attr Name        Size                     Bytes  Class
==== ====        ====                     =====  ===== 
    a            1x1                          8  double
    b            1x1                          8  double
    prefix       1x16                        16  char

Total is 18 elements using 32 bytes.

Julia Solutions


Solution 1 - Julia

An Update:

whos() 

... is not working either in iJulia or at the command prompt in Julia-1.0.0.

It is working in Julia-0.6.4, though.

On the other hand,

varinfo()

....prints information about the exported global variables in a module. For Example,

julia-1.0> varinfo()
name                    size summary                        
–––––––––––––––– ––––––––––– –––––––––––––––––––––––––––––––
Base                         Module                         
Core                         Module                         
InteractiveUtils 154.271 KiB Module                         
Main                         Module                         
PyPlot           781.872 KiB Module                         
ans               50.323 KiB Plots.Plot{Plots.PyPlotBackend}
myrepl               0 bytes typeof(myrepl)                 
x                   88 bytes 1×6 Array{Int64,2}             
y                    0 bytes typeof(y)                      

Hope, this is found useful.

Solution 2 - Julia

You can use Julia's whos functions just like that Matlab command.

julia> whos()
Base                          Module
Core                          Module
Main                          Module
ans                           Nothing

julia> x = 5
5

julia> whos()
Base                          Module
Core                          Module
Main                          Module
ans                           Int64
x                             Int64

Any modules (packages/libraries) you import into your local scope (using using) will also show up in the list (as Modules, like Base, Core, and Main above).

Additionally, you can ask about names exported by Modules. Base is the module containing the standard library.

julia> whos(Base)
!                             Function
!=                            Function
!==                           Function
$                             Function
%                             Function
&                             Function
*                             Function
+                             Function
.... (lots and lots more)

Considering that that result scrolls way off my screen, you can understand why you'd want to filter the results. For that you can use Regexes. (For more info on Julia's regexes, see this manual section)

julia> whos(r"M")
Main                          Module

julia> whos(Base, r"Match"i)
DimensionMismatch             DataType
RegexMatch                    DataType
each_match                    Function
eachmatch                     Function
ismatch                       Function
match                         Function
matchall                      Function

I wasn't aware of the whos function before you asked, so thanks for helping me learn something new too. :)

Julia issue #3393 on github is about adding memory sizes to the whos output. It also references making whos return a value rather than just printing the information out.

Solution 3 - Julia

Not sure if there is something better, but

names(Main)[4:end]

seems to work. The [4:end] part is because it includes :Main, :Core and :Base which I think you would not want. I hope they will always be at the beginning.

Solution 4 - Julia

whos() is not available in newer versions of Julia (1.0 onward). Use varinfo() instead. For example, varinfo(Core,r".*field.*")

Solution 5 - Julia

As of version 1.1 there is also the @locals macro

> The experimental macro Base.@locals returns a dictionary of current local variable names and values

Release notes

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionvinu k nView Question on Stackoverflow
Solution 1 - JuliaLong shortView Answer on Stackoverflow
Solution 2 - JuliaastrieannaView Answer on Stackoverflow
Solution 3 - JuliaivarneView Answer on Stackoverflow
Solution 4 - JuliateuView Answer on Stackoverflow
Solution 5 - JuliaTimDView Answer on Stackoverflow