MathGroup Archive 1995

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Variable Listing?

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg1036] Re: Variable Listing?
  • From: wagner at bullwinkle.cs.Colorado.EDU (Dave Wagner)
  • Date: Wed, 10 May 1995 06:45:31 -0400
  • Organization: University of Colorado, Boulder

In article <3ocggf$o3o at news0.cybernetics.net>,
Paul A. Rubin <rubin at msu.edu> wrote:
>In article <3o9n01$b92 at news0.cybernetics.net>,
>   deslover at ssec.wisc.edu (Daniel DeSlover) wrote:
>->Is there a way in Mma to view all variables that have been
>->defined during a current session?  For example, in Matlab
>->I can use the "whos" command to get this display:
>->
>->
>->>> whos
>->             calib     431 by 5          2155     17240       Full      
>->          datedata       1 by 3             3        24       Full      
>->               day       1 by 1             1         8       Full      
>->              mass     953 by 1           953      7624       Full      

(stuff deleted) 

>->Such a command in Mma would be nice if it doesn't exist.
>->
>->dan
>->deslover at vil.ssec.wisc.edu
>
>You can get a list of variable *names* fairly easily.  Names["*"] will list 
>every defined symbol (including kernel symbols).  Names["@*"] will list 
>every symbol except those starting with an upper case letter.  This screens 
>out most Mma keywords (which tend to begin with an upper case letter), but 
>not the ones beginning with "$".  The best approach, if you can discipline 
>yourself accordingly, is never to work in the Global context.  If you start 
>your session by setting a new context (e.g., Begin["Work"]), then you can 
>limit the name list to your context (Names["Work`*"]).
>
>* Paul A. Rubin                                  Phone: (517)

Not necessary Paul; if you restrict the search with Names["Global`*"]
then you will get the names of only symbols that you have defined, not
the names of any kernel symbols (all of which live in the System`
context

In[1]:=
    calib = Table[0, {431}, {5}];

In[2]:=
    datedata = Table[0, {1}, {3}];

In[3]:=
    day = 0;

In[4]:=
    mass = Table[0, {953}, {1}];

In[5]:=
    Names["Global`*"]
Out[5]=
    {calib, datedata, day, mass}


Here's how to get most of the information that Dan wants. First, note
that the elements returned by Names are strings, not symbols:

In[6]:=
    InputForm[%]
Out[6]//
InputForm=
    {"calib", "datedata", "day", "mass"}

So we map the following function onto this list:

In[7]:=
    info[name_String] :=
    Module[{sym, dim},
	    sym = ToExpression[name];
	    dim = Dimensions[sym];
	    { name, dim, Times@@dim, ByteCount[sym]}
    ]

In[8]:=
    info["calib"]
Out[8]=
    {calib, {431, 5}, 2155, 43116}

In[9]:=
    info /@ Names["Global`*"] // TableForm
Out[9]//TableForm=
	       431
    calib      5     2155   43116

	       1
    datedata   3     3      84

    day              1      12

    dim              1      0

    dim$             1      0

    info             1      0

	       953
    mass       1     953    34324

    name             1      0

    sym              1      0

    sym$             1      0

Those symbols "info", "dim", etc. came from the definition of info itself.

In[10]:=
    Names["Global`*"]
Out[10]=
    {calib, datedata, day, dim, dim$, info, mass, name, sym, sym$}

In[11]:=
    Remove[info, dim, sym]

In[12]:=
    Names["Global`*"]
Out[12]=
    {calib, datedata, day, mass, name}

This is kind of ugly, but we can eliminate all but "info" by putting the
implementation of the function into another context:
In[13]:=
    info::usage = "info[s] returns the name, dimensions,
     total size, and byte count for the symbol named s.";

In[14]:=
    Begin["Private`"];
    info[name_String] :=
    Module[{sym, dim},
	    sym = ToExpression[name];
	    dim = Dimensions[sym];
	    { name, dim, Times@@dim, ByteCount[sym]}
    ]
    End[];

In[17]:=
    Names["Global`*"]
Out[17]=
    {calib, datedata, day, info, mass, name}

It still works like before.  You could even hide the name "info" in the
private context, but then you'd have to refer to it as Private`info
whenever you wanted to call the function.

		Dave Wagner
		Principia Consulting
		(303) 786-8371
		princon at csn.net
		http://www.csn.net/princon


  • Prev by Date: Remote kernel connection
  • Next by Date: Printing (to the _printer_) during mma execution?
  • Previous by thread: Variable Listing?
  • Next by thread: Re: Variable Listing?