10

I'm trying to set up some notebooks so that I have one "seed" notebook that defines all my values globally when I run it. I have three other notebooks that take these values and run with them to do what they do.

Problem is, these notebooks solve for functions and define them locally. I can't re-run these notebooks without running the seed book every time since Solve sees functions that are already defined. Is there a way to clear JUST the variables I have defined in a notebook?

Edit: forgot to mention the seed notebook does a ClearAll["Global``*"] at the beginning.

SixtySuit
  • 163
  • 1
  • 6
  • 4
    I was trying to figure out what are "just variables"... Are these immanently just, or they become so when used by just programmers? I wonder who uses then the dishonest symbol table. – István Zachar Nov 07 '12 at 17:51

2 Answers2

12

The Global` context in a kernel is shared between notebooks communicating with that kernel, so issuing a ClearAll["Global`*"] in one notebook will clear them from all notebooks.

As an alternative, you can define your "seed" notebook variables in a specific context (e.g. seed`) and then add that context to the global path. For example, in your seed notebook,

Begin["Seed`"];
    foo = 1;
    bar = 2;
End[];

AppendTo[$ContextPath, "Seed`"];

Now you can use these variables in your secondary notebooks without having to expressly qualify them as Seed`foo and simply do ClearAll["Seed`*"] to clear only these variables.


You can also set the seed notebook to have a unique context by choosing Evaluation > Notebook's Default Context > Unique to this Notebook, which will then make its context something like Notebook$$12345` and you can append this to $ContextPath as above. However, having such a context name is both unsightly and not informative, so I'd suggest using explicitly (and appropriately) named contexts.

rm -rf
  • 88,781
  • 21
  • 293
  • 472
  • Hmmm... Sounds like I need to educate myself in the use of Contexts. Your suggestion is a good one, but from what I can tell it's doing the opposite of what I need--allowing me to clear Seed when I want to clear, say, Case1 for my notebook test case. Thanks though; it's a good start. – SixtySuit Nov 07 '12 at 19:42
  • @SixtySuit Uh? I don't understand what you mean... If you need Case1, then make Case1 a separate context. – rm -rf Nov 08 '12 at 16:46
2

It's a bit of a workaround but you can:

1) list all the Global variables like so: Names["Global`*"]

2) search you current notebook for specific strings

By searching your notebook, returning a list of all the positions where global variables are used and then searching for which instances there is an "=" directly after that variable you can get a list of all variables defined in the current notebook:

Take[Names["Global`*"], 
 Flatten[Position[{Off[StringTake::mseqs]; 
   StringTake[
      ToString[
       FindList[
        NotebookFileName[], #]], {Max[Flatten[
          StringPosition[FindList[NotebookFileName[], #], #]]] + 6}]
         & /@ Names["Global`*"], On[StringTake::mseqs]}[[1]], 
 "="]]]
gpap
  • 9,707
  • 3
  • 24
  • 66
Aart Goossens
  • 1,638
  • 12
  • 21
  • 1
    It's a decent start, but there are several ways to define variables. For instance, :=, /:, :^=, etc. You can also define several variables at once, as {foo, bar, baz} = ... Also, there's the possibility of strings such as "foo = ..." showing up as false positives. – rm -rf Nov 08 '12 at 16:43