From: Szabolcs Horvát on 22 Jul 2010 05:40 Hello MathGroup, I am looking for a simple and (code-wise) efficient way to display a list of variables (simple symbols) together with their values in a table. My Mathematica skills got a little rusty, I was wondering what is a simpler / better way to achieve it than this: SetAttributes[dynamicVariableDisplay, HoldAll] dynamicVariableDisplay[vars__] := Dynamic(a)Grid[ Transpose[{{Function[x, SymbolName[Unevaluated[x]], {HoldAll}] /@ Unevaluated[vars]}, {vars}}], Alignment -> {{Left, "."}}] a=1; b=2; c=3; dynamicVariableDisplay[a, b, c] There's too much juggling with unevaluated symbols. Thanks for the help in advance, Szabolcs
From: Leonid Shifrin on 23 Jul 2010 07:11 Hi Szabolcs, > > > My Mathematica skills got a little rusty You are kidding, right? Seriously, I don't see an easy solution which would be much simpler conceptually - looks like some number of Unevaluated will be needed anyways in this approach. You can save some keystrokes by using Block trick though: ClearAll[dynamicVariableDisplayAlt]; SetAttributes[dynamicVariableDisplayAlt, HoldAll]; dynamicVariableDisplayAlt[vars__] := Dynamic(a)Grid[Block[{vars}, {SymbolName[#], #} & /@ {vars}], Alignment -> {{Left, "."}}]; Here I exploited the way name conflicts are resolved when RuleDelayed is the outer scoping construct - namely that it does not respect inner scoping constructs (Block here). So, while you get a warning by {vars} painted in red inside Block, it will work all right. You can also make this a pure function, which is a little more compact: dynamicVariableDisplayPure = Function[Null, Dynamic(a)Grid[Block[{##}, {SymbolName[#], #} & /@ {##}], Alignment -> {{Left, "."}}], HoldAll]; This example is actually quite interesting, since here, while Function is a lexical scoping construct, with slots is shows macro-like behavior. And I found very puzzling why this version with fixed number of named arguments also works: dynamicVariableDisplayPure3Args = Function[{x, y, z}, Dynamic(a)Grid[Block[{x, y, z}, {SymbolName[#], #} & /@ {x, y, z}], Alignment -> {{Left, "."}}], HoldAll]; For this, I have no explanation. I would expect Function to respect Block and therefore the names of the variables to be {"x","y","z"} and not {"a","b","c"}. But Function acted as ruthlessly as RuleDelayed here. Actually, the same can be seen on a much simpler example Function[x, Block[{x}, Print[x]]][1]. Regards, Leonid , I was wondering what > is a simpler / better way to achieve it than this: > > SetAttributes[dynamicVariableDisplay, HoldAll] > dynamicVariableDisplay[vars__] := > Dynamic(a)Grid[ > Transpose[{{Function[x, SymbolName[Unevaluated[x]], {HoldAll}] /@ > Unevaluated[vars]}, {vars}}], Alignment -> {{Left, "."}}] > > a=1; b=2; c=3; > > dynamicVariableDisplay[a, b, c] > > There's too much juggling with unevaluated symbols. > > Thanks for the help in advance, > Szabolcs > >
|
Pages: 1 Prev: Strange 26base enumeration Next: Very very basic question about Mathematica |