9

If I set up

a = 1

Is there any command to get "a" by typing value 1?

Another example.

If

x = 3

y = 7

z = 6

then, Can I get 'xyz' by typing something like {3,7,6}?

David G. Stork
  • 41,180
  • 3
  • 34
  • 96
S. Lee
  • 91
  • 1

1 Answers1

13
a = 1;

Select[Names["Global`*"], Symbol[#] == 1 &]
{"a"}

Notice it may not be robust if you have a habit of writing procedures with OwnValues, e.g.: c := NotebookClose @ EvaluationNotebook[] etc.

ClearAll[a]
a := Echo["this is only echo but it may be something more damaging"];
b = 1;

Select[Names["Global`*"], Symbol[#] == 1 &]
>> this is only echo but it may be something more damaging

{"b"}

To avoid this you can scan OwnValues instead of using Symbol/ToExpression:

Select[
  Names["Global`*"]
, ToExpression[
    #
  , StandardForm
  , Function[sym, MemberQ[OwnValues[sym], _ :> 1], HoldFirst]
  ] &
]
{"b"}
Kuba
  • 136,707
  • 13
  • 279
  • 740