Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.1K Views
|
2 Replies
|
1 Total Like
View groups...
Share
Share this post:

[?] Assign a list of symbols?

Posted 7 years ago

Given a list of symbols syms={x,y}, I want to assign it a list of values.

Of course I can write:

x =.; x = 4711; Print[x];
y =.; y = 4712; Print[{x, y}];
4711
{4711,4712}

Or:

x =.; y =.;
{x, y} = {711, 712}; Print[{x, y}];
{711,712}

But I cannot write (well, I can, but it does not assign x and y):

x =.; y =.; syms = {x, y}; Print[syms, ", ", {x, y}];
syms = {11, 12}; Print[syms, ", ", {x, y}];
{x,y}, {x,y}
{11,12}, {x,y}

I think I understand the latter. Now I try with SymbolNames. This gives errors which I don't understand.

x =.; Symbol["x"] = 11; Print[x];
Set::write: Tag Symbol in Symbol[x] is Protected.
x

What I really want is something like:

x =.; y =.; symNames = {"x", "y"}; Print[symNames // InputForm];
(Symbol /@ symNames) = {1, 2}; Print[{x, y}];
{"x", "y"}
Set::write: Tag Map in Symbol/@{x,y} is Protected.
{x,y}
POSTED BY: Werner Geiger
2 Replies
Posted 7 years ago

Thanks, Rohit, for your answer. This works indeed:

x =.; y =.; symNames = {"x", "y"}; Print[symNames // InputForm];
Evaluate[Symbol /@ symNames] = {1, 2}; Print[{x, y}];
{"x", "y"}
{1,2}

I think I tried many things including Evaluate, but I got more and more confused.

POSTED BY: Werner Geiger
Posted 7 years ago

Hi Werner,

You need to Evaluate before the Set.

Evaluate[Symbol /@ symNames] = {1, 2}
(* {1, 2} *)
POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard