1

I want to get the name of a variable instead of its value in output. Say I have a function in which I have vectors e={1,0,0,1} and f={1,1,0,1}, and the input for the function is a symbol (e or f). Now, in output, I want to make a grid which shows the symbol entered in input in one row, and its value in the second row. How would I do that? I tried the function HoldForm, but that did not work.

Drobdien
  • 77
  • 4

2 Answers2

2

HoldForm[] is surely an important ingredient. The other important bit is that whatever function you write should have an attribute like HoldFirst or HoldAll to prevent premature evaluation. As a simple example:

SetAttributes[gg, HoldFirst];
gg[x_Symbol] := Column[{Row[{HoldForm[x], "="}], x}]

Then,

ff = {1, 1, 0, 1};

gg[ff]
   ff=
   {1, 1, 0, 1}
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
  • I tried this and it doesn't seem to work, which is probably my fault. If we just forget the grid or any formatting, just a way to get a variables name instead of its value, perhaps as a string. How would I do that? – Drobdien May 08 '20 at 17:13
  • 2
    "it doesn't seem to work" - Can you try this on a fresh kernel, then? – J. M.'s missing motivation May 08 '20 at 17:21
2
e = {1, 0, 0, 1};
SymbolName[Unevaluated[e]]

Result:

e

Another option, if to be used inside a function (as noted in your comment), would be to design the function to accept the symbol name instead of the symbol (or require both to make things clearer). Here is a version with just the symbol name. The symbol is extracted in the function.

x = 5;
Plus5[symbolname_] := 
 (s = Symbol[symbolname];
     s = s + 5;
   Return[{symbolname, s}])

Plus5["x"]

This returns:

{x, 10}
Jean-Pierre
  • 5,187
  • 8
  • 15
  • 1
    It does not work if I enter it through a function. The function has an input, let's call it sign_, then I have the e vector inside of the function. So my goal is to input function[e], and get e in the output later. – Drobdien May 08 '20 at 20:09
  • The Symbol function was what finally solved this problem, thank you very much! – Drobdien May 09 '20 at 13:48