1
$\begingroup$

Given a numerical value, is it possible to check whether that value is stored in (or defines) some variable in the notebook?

Maybe if I could list all of the defined variables, then I could simply select the one(s) that equal the value. Is this possible?

I feel it should be possible, but I don't know how to approach it. For example, whenever I set

x = 2; y = 3; z = 3;

I wonder if I can run some command such that

 In[] := GetDefinedVariables[]
Out[]  = {x, y, z}

or

 In[] := GetDefinedVariables[]
Out[]  = {{x,2}, {y,3}, {z,3}}

Ideally, I would like a function to do the following

 In[] := GetCorrespondingVariables[{2,3,4}]
Out[]  = {x, {y,z}, Null}

Could this also work for non-numerical variables?

$\endgroup$
  • 1
    $\begingroup$ ?Global`* will list everything in the Global context which will include the variables you have defined and more. If you define variables in another context you can check that also. $\endgroup$ – Bill Watts Mar 30 '20 at 17:21
  • $\begingroup$ x = 2; y = 3; z = 3; {#, ToExpression@#} & /@ Names["Global`*"] $\endgroup$ – Bob Hanlon Mar 30 '20 at 18:11
1
$\begingroup$

You can use Names to list the variables and OwnValues to get the value that they store. Namely

GetDefinedVariables[] := Module[{allrules},
   allrules = ToExpression["OwnValues[" <> # <> "]"] & /@ Names["Global`*"];
   Replace[allrules /. RuleDelayed -> Sequence, {} -> Nothing, 1]
];

The second line simply puts the result into tuples, as you specified, and removes the variables with no definitions (that is because if you use a variable anywhere, even without assigning it a value, Mathematica will still save it into the "Global`" context).

Then you can simply select the variables that match the value you input.

GetCorrespondingVariables[{x__}] := Module[{defd},
  defd = GetDefinedVariables[];
  Table[Select[defd, #[[2]] == val &][[All, 1]], {val, {x}}]
];

This is slightly different than your example in that the case with no variables found returns {} and not Null.

Furthermore you'll notice all HoldPattern wrapping the variables, that's because otherwise the normal evaluation process would evaluate them to their OwnValue. You can alternatively turn the symbol into strings.

And yes, this works also for non numerical values. However I would advise you to change the == into a === if you want to compare more complicated objects.

$\endgroup$
  • $\begingroup$ This looks great. I will take a closer look tomorrow. $\endgroup$ – sam wolfe Mar 31 '20 at 0:47
  • $\begingroup$ And by the way, nice to meet you Mr Calavera, still in Rubacava? $\endgroup$ – sam wolfe Mar 31 '20 at 0:48
  • $\begingroup$ Yes, I am! It took me forever to get an internet connection from here. $\endgroup$ – MannyC Mar 31 '20 at 7:37
  • 1
    $\begingroup$ What a lovely place. Hope you find Miss Colomar soon, she is one of a kind $\endgroup$ – sam wolfe Mar 31 '20 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.