0
$\begingroup$

Is it possible to first perform an operation on LHS of the = symbol, evaluate that, and then assign the RHS to the new LHS as usual? The following shows the most basic example I could think of.

ToExpression["list" <> ToString[4]] = {1,2,3,4,5});

> Set::write: Tag ToExpression in ToExpression[list4] is Protected.

It looks like Mathematica thinks we want to redefine the functions used on the LHS of =, not first evaluate it first and then assign. I've tried surrounding LHS with brackets and parenthesis, but that doesn't work either. Is there a way to make this work? If yes, with what code?

The reason this arose is because I made a function that takes input n, and makes a list accordingly. Now I wanted to AUTOMATICALLY give that list a name that has n in it. So if I compute f[1209], I want to automatically store whatever list I computed to list1209, without having to type list1209 = computedlist.

$\endgroup$
2
  • 1
    $\begingroup$ Welcome to MSE. Try Evaluate@ToExpression["list" <> ToString[4]] = {1, 2, 3, 4, 5} $\endgroup$ Jan 29 '20 at 4:20
  • 1
    $\begingroup$ In this case, it's safer to use something like Symbol["list" <> IntegerString[4]]. $\endgroup$
    – J. M.'s torpor
    Jan 29 '20 at 4:44

Browse other questions tagged or ask your own question.