In addition to Mr.Wizard's answer here is a collection of other possibilities:
data1 = {1, 1};
data2 = {2, 2};
datalist := {data1, data2};
ToString /@ Map[HoldForm, OwnValues[datalist], {3}][[1, 2]]
ToString /@ Map[Unevaluated, OwnValues[datalist], {3}][[1, 2]]
ToString /@ Thread[HoldForm[datalist] /. OwnValues[datalist]]
ToString /@ Thread[Extract[OwnValues[datalist], {1, 2}, HoldForm]]
ToString /@ Thread[Extract[OwnValues[datalist], {1, 2}, Unevaluated]]
ToString /@ Thread[Rest@Level[OwnValues[datalist], {2}, HoldForm]]
ToString /@ Thread[Rest@Level[OwnValues[datalist], {2}, Unevaluated]]
Unevaluated[Block[datalist, ToString /@ datalist]] /. OwnValues[datalist]
Cases[OwnValues[datalist], s_ :> Block[{s}, ToString@s], {3}] // Rest
Cases[OwnValues[datalist], s : {___} :> Block[s, ToString /@ s], {2}][[1]]
FirstCase[OwnValues[datalist], s : {__} :> Block[s, ToString /@ s], {}, {2}]
Cases[OwnValues[datalist], s : {___} :> ToString /@ Thread[HoldForm@s], {2}][[1]]
FirstCase[OwnValues[datalist], s : {__} :> ToString /@ Thread[HoldForm@s], {}, {2}]
{"data1", "data2"}
{"data1", "data2"}
{"data1", "data2"}
{"data1", "data2"}
{"data1", "data2"}
{"data1", "data2"}
{"data1", "data2"}
{"data1", "data2"}
{"data1", "data2"}
{"data1", "data2"}
{"data1", "data2"}
{"data1", "data2"}
{"data1", "data2"}
All the above methods work correctly when datalist
is empty:
datalist := {};
ToString /@ Map[HoldForm, OwnValues[datalist], {-1}][[1, 2]]
{}
listnames = Flatten[StringCases[ ToString /@ {OwnValues@data1, OwnValues@data2}[[All, 1, 1]], "HoldPattern[" ~~ x__ ~~ "]" :> x]]
? $\endgroup$Names["data@"]
gives you a list of strings:{"data1", "data2", "datalist"}
. $\endgroup$