# Why does it fail to assign values to variable in Grid? [closed]

I have a grid such as the following, and want to assign the values in the second row to the variable in the first row, but it always fail, can you provide a solution? Thank you!

constant =
Grid[{{a, b, c, d, e, f, g}, {1, 2, 3, 4, 5, 6, 7}}, Frame -> All]
constant[[1, 1]] // FullForm
constant[[1, 2]] // FullForm
constant[[1, 1]] = constant[[1, 2]]
a
b
c


The results are:

They are not what I want a=1, b=2... Why?

But if I use {a, b, c, d, e, f, g}={1, 2, 3, 4, 5, 6, 7}, it will succeed.

• Grid is a display wrapper, don't use it to define things as it interferes with everything. Just for display. – Roman Jun 6 '19 at 11:20
• You are modifying constant if you want to set a etc then try Evaluate[constant[[1, 1]]] = constant[[1, 2]] but it will only work when a and friends don\t have values/ – Kuba Jun 6 '19 at 11:22
• @Roman, Because I have a lot of constant values used in a program, and the program can be used repeatedly only the constant are different, so I want to find a intuitive way to change the constant, the Grid not only can display the values while expressing their one-to-one correspondence, but also give a easy way to modify their values later. Thank you for your attention! – likehust Jun 6 '19 at 12:54
• @Kuba, Your method basically solved my problem, only the constant was damaged which will not display a,b,c,d.... but their values 1,2,3.... However, in general, this is an acceptable answer. Thanks. – likehust Jun 6 '19 at 13:16

As mentioned by @Roman, you shouldn't use display wrappers (such as Grid, MatrixForm, etc) in definitions of anything that you want to use in subsequent calculations.

Aside from that, here are two options:

One is to store the names of the variables as strings.

ClearAll["Global*"]
vars = {a, b, c, d, e, f, g};
values = {1, 2, 3, 4, 5, 6, 7};
labels = Map[ToString, vars];


Set the variables to their corresponding values:

Thread[Evaluate[vars] = values]

{a, b, c}
(* Out[]= {1, 2, 3} *)


You can then show the grid using the variable names stored in labels.

Grid[{labels, values}, Frame -> All]


Another option is to avoid setting the variables altogether, and use them to construct rules instead.

ClearAll["Global*"]
vars = {a, b, c, d, e, f, g};
values = {1, 2, 3, 4, 5, 6, 7};

Grid[{vars, values}, Frame -> All]