2

How do I get the list of all OwnValues of symbols in the Global` context?

All my attempts have failed because OwnValues insists taking only Symbol, and Names[] only returns a list of String:

ClearAll["Global`*"]
a = 3;
OwnValues[Evaluate[Symbol[#]]] & /@ Names["Global`*"]

OwnValues::sym : Argument 3 at position 1 expected to be a symbol

{OwnValues[3]}

Converting the String to Symbol causes the Symbol to evaluate before it enters OwnValues.

QuantumDot
  • 19,601
  • 7
  • 45
  • 121
  • 2
    Somebody will find the duplicate, but in the mean time, you can use: Cases[Names["Global`*"], x_ /; ToExpression[x, InputForm, OwnValues]=!={}] – Carl Woll Jul 15 '17 at 18:28
  • 2
    Also, Language`ExtendedDefinition is a useful function that accepts strings and returns a list of *Values plus a few other pieces of information. – Carl Woll Jul 15 '17 at 18:32

1 Answers1

2

Using the same code structure, for example, you can use the fact that MakeExpression returns HoldComplete:

ClearAll["Global`*"]
a = 3;
ReleaseHold@Hold[OwnValues][MakeExpression[#]] & /@ Names["Global`*"]

{{HoldPattern[a] :> 3}}

edit

Then using b3m2a1's suggestion,

Join@@ToExpression[Names["Global`*"], StandardForm, OwnValues]

returns the desired list.

QuantumDot
  • 19,601
  • 7
  • 45
  • 121
Stitch
  • 4,205
  • 1
  • 12
  • 28