Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #6466 > unrolled thread

Re: Keeping track of variables used in Mathematica

Started byDana DeLouis <dana01@me.com>
First post2011-11-16 09:46 +0000
Last post2011-11-17 12:03 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: Keeping track of variables used in Mathematica Dana DeLouis <dana01@me.com> - 2011-11-16 09:46 +0000
    Re: Keeping track of variables used in Mathematica David Bailey <dave@removedbailey.co.uk> - 2011-11-17 12:03 +0000

#6466 — Re: Keeping track of variables used in Mathematica

FromDana DeLouis <dana01@me.com>
Date2011-11-16 09:46 +0000
SubjectRe: Keeping track of variables used in Mathematica
Message-ID<ja00p0$61k$1@smc.vnet.net>
>   a list of custom variables assigned and used in a current notebook 

Hi.  I don't have a great solution for the current notebook.

If I have a first workbook with just :
a = 2;
b = 4;

And then a second (the Current Workbook) with:

c=6;

Names["Global`*"]
{a,b,c}

Then this returns the Global names from ALL workbooks, and not from the Current Workbook. (ie Just c)

There was a suggestion for $NewSymbol, but I had mixed results.
Did you or anyone else get this idea to work.  Here's what I mean.
At the top of a third workbook, the best I came up with is the following:

NewNames={};
$NewSymbol=AppendTo[NewNames,#1]&;

d=3;
e=Range[4];

(* Global has everything *)

Names["Global`*"]
{a,b,c,d,e,NewNames}

(* But NewNames just had d & e, which seems like what we want *)

NewNames
{d,e}

$NewSymbol=.

However, one of the drawbacks seems to be that if you recalculate the workbook, then NewNames is reset to {}, and nothing is added since the symbols (d & e) already exists.
Does anyone know of the better way to use this function in this manner?

If I start yet another workbook, then this is perhaps another option.

e=7;
f=Range@10;
Block[{x,y,z}, y=5.5];
g[x_]:=x^2

Union[Flatten[Apply[List,Extract[First[Apply[List,NotebookGet[],{1}]],Position[First[Apply[List,NotebookGet[],{1}]],RowBox[{_,"=",_}],\[Infinity]]],{1}],1][[All,1]]]

{e,f,y}

Names["Global`*"]
{a,b,c,d,e,f,g,NewNames,x,y,z}

Here, g,x,y,z became global variables,  but the above code only picked up
e,f,y  as they were assigned values - within this current notebook.

This is not fully tested of course.
Perhaps there are better ways ???

= = = = = = = = =
Here are the steps I used above.
I combined them above to remove the variables.
I didn't want to mix these variables with the workbook variables.

t=First[Apply[List,NotebookGet[],{1}]];

pos=Position[t,RowBox[{_,"=",_}],\[Infinity]];

Union[Flatten[Apply[List,Extract[t,pos],{1}],1][[All,1]]]

= = = = = = = = =
HTH  :>)
Dana DeLouis
= = = = = = = = =



On Nov 10, 6:56 am, Mark <big.d...@att.net> wrote:
> Hello community:
> 
> Does Mathematica have a command that will output or compute a list of custom variables assigned and used in a current notebook / Mathematica session?
> 
> If so, what is the command / expression?
> Thanks in advance
> Mark


[toc] | [next] | [standalone]


#6498

FromDavid Bailey <dave@removedbailey.co.uk>
Date2011-11-17 12:03 +0000
Message-ID<ja2t79$n4u$1@smc.vnet.net>
In reply to#6466
On 16/11/2011 09:46, Dana DeLouis wrote:

>
> There was a suggestion for $NewSymbol, but I had mixed results.

I made that suggestion

> Did you or anyone else get this idea to work.  Here's what I mean.
> At the top of a third workbook, the best I came up with is the following:
>
> NewNames={};
> $NewSymbol=AppendTo[NewNames,#1]&;

You could try:

If[! ValueQ[NewNames],NewNames={}]

This will only set it once, even if you re-evaluate the notebook. (BTW, 
you really should avoid fully capitalised names like NewNames - such 
names are reserved for future Wolfram functions).

I would also restrict the collection to global symbols:

$NewSymbol=If[#2=="Global`",AppendTo[NewNames,#1]]&;

>
> d=3;
> e=Range[4];
>
> (* Global has everything *)
>
> Names["Global`*"]
> {a,b,c,d,e,NewNames}
>
> (* But NewNames just had d&  e, which seems like what we want *)

I am wondering if the other symbols were spuriously added by some 
packages that get loaded in your environment. I usually pre-load some 
packages, but on a bare mathematica, the Global context appears to be empty.
>

Extracting the names from the BoxForm in a notebook doesn't seem very 
useful to me - because it doesn't pick up the context of symbols with an 
implicit context.

David Bailey
http://www.dbaileyconsultancy.co.uk


[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web