Suppose I have a few symbols, one of which has a value:
{abc1, abc2 = 5, abc3};
I can use Names to get the list of names, as strings:
InputForm[names = Names["Global`abc*"]]
(* {"abc1", "abc2", "abc3"} *)
Now I want to find which symbols have values. This fails, because ValueQ expects the first argument to be a Symbol, not a String:
Select[names, ValueQ]
(* {} *)
This fails (with lots of messages), because ValueQ doesn't evaluate the argument enough:
Cases[names, st_ /; ValueQ[Symbol[st]]]
(* {"abc1", "abc2", "abc3"} *)
If we force evaluation, we go too far, and this fails because we get ValueQ[5] instead of ValueQ[abc2]:
Cases[names, st_ /; ValueQ[Evaluate[Symbol[st]]]]
(* {} *)
This approach works, but is far from elegant:
Cases[names, st_ /; ToExpression["ValueQ[" <> st <> "]"]]
(* "abc2" *)


ValueQis not innocent - it leaks evaluation. Here is some discussion, where I also contributed an answer with what is supposed to be a safer version ofvalueQbased on one-step evaluation: http://stackoverflow.com/questions/4599241/checking-if-a-symbol-is-defined/4621562#4621562. It is overly complex, I know it can be written better, but the point is to be wary of the systemValueQ, to avoid nasty surprises. – Leonid Shifrin Jan 18 '12 at 20:55ValueQis safe for as long as it's acting on a symbol only (i.e. is looking atOwnValues), but it evaluates the argument for anything else. Is this correct? I remember I looked at the implementation ofValueQonce (it's accessible) and concluded this, but that was a long time ago. – Szabolcs Jan 18 '12 at 21:14OwnValuesonly, there is a bullet-proof solution:(HoldComplete[sym]/.OwnValues[sym])=!=HoldComplete[sym]- very simple and robust. – Leonid Shifrin Jan 18 '12 at 21:25