Purpose
To use variable name as file name conveniently.
Code
Special thanks to @J.M.'s tips!
Remove@"Global`*"
p = Plot[#, {x, 0, 1}] & /@ {x, 1/x};
SetAttributes[g, HoldAll]
g[p_] := Export[ToString@Unevaluated@p <> ".png", p]
(* Few images: *)
{g@p[[1]], g@p[[2]]}
(* Many images: *)
g /@ Hold @@
Table["p[[" <> ToString@i <> "]]", {i, Length@p}] //
ToString // ToExpression // ReleaseHold
Question
I'm not satisfied with this two lines in the last section of the above code:
Table["p[[" <> ToString@i <> "]]", {i, Length@p}] //
ToString // ToExpression
I believe there are smarter ways to implement that, please teach me, thanks!
pno longer contains the symbolsp1orp2, which is why you get the results you see when mapping overp. UsingHold[p1, p2]might be more helpful to you. – J. M.'s missing motivation Mar 10 '18 at 10:21ReleaseHold[]is for:ReleaseHold[{g /@ Hold[p1, p2]}]. Again,InputForm[p]will reveal at once that you can't usepto refer top1andp2. – J. M.'s missing motivation Mar 10 '18 at 10:30g[]:p = Hold[p1, p2]; ReleaseHold[{g /@ p}]– J. M.'s missing motivation Mar 10 '18 at 21:54