TransWikia.com

Is there a workaround to dereference a list element that's the name of an undefined symbol, and iteratively assign values to the symbols in the list?

Mathematica Asked by Dustin Darcy on January 1, 2021

The title mostly covers what I would like to do. The image below shows in some situations it is possible dereference an element in a list using Evaluate to assign a value:

a[[0]]
   (*Symbol*)
vars = {a, b, c}
Evaluate[vars[[1]]] = 1
{a[[0]], a}
   (*{Integer, 1}*)

However, trying to do this for several items in a list with an explicit pure function map doesn’t fair as well (likely because "The expression #1 cannot be used as a part specification"):

ClearAll[a, b, c]
Evaluate[vars[[#]]] = # & /@ Range[3]
   (*Part::pkspec1: The expression #1 cannot be used as a part specification. >>*)
   (*Set::setps: {a,b,c} in the part assignment is not a symbol. >>*)
   (* ... compared to below which works ... *)
Evaluate[vars[[#]]] == # & /@ Range[3]
   (*{a == 1, b == 2, c == 3}*)

Ideally I’d like to use MapThread for this particular task, but that doesn’t appear to work either:

MapThread[Evaluate[[#1]] = #2 &, {vars, {1, 2, 3}}]
   (* Set::pkspec1: The expression #1 cannot be used as a part specification. >> *)

Any tips or workarounds on how to pull something like this off?

Thanks for any help!

2 Answers

(Evaluate@vars[[#]] = #) & /@ Range[3]

Correct answer by asd1dsa on January 1, 2021

Rather than MapThread, I recommend Do for the more general case where the values are also taken from a list. Like so:

Clear[a, b, c]
vars = {a, b, c};
vals = {27, 16, 32};
Do[Evaluate[vars[[i]]] = vals[[i]], {i, Length @ vars}];
vars
{27, 16, 32}

Answered by m_goldberg on January 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP