Message Boards Message Boards

0
|
9762 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How can I assign multiple values to multiple variables at once?

Posted 9 years ago

I need to assign the numbers 1-26 to the letters a-z. Why can't I type Alphabet[]=Range[26] ?

Thank you so much for your help! :)

POSTED BY: Florian Wicher
4 Replies
Posted 9 years ago
Thread[ToExpression /@ Alphabet[] == Range@26]
{a == 1, b == 2, c == 3, d == 4, e == 5, f == 6, g == 7, h == 8, 
 i == 9, j == 10, k == 11, l == 12, m == 13, n == 14, o == 15, 
 p == 16, q == 17, r == 18, s == 19, t == 20, u == 21, v == 22, 
 w == 23, x == 24, y == 25, z == 26}

Maybe this one you are looking for?

POSTED BY: Okkes Dulgerci
Posted 9 years ago

Thank you, already helped me out a lot, but I immediately stumbled upon the next problem when trying to delete the assignment again: I tried it writing

 Evaluate[Alphabet[]] =.

but if I am right, Mathematica now works with the values I assigned to the variables, and not the variables themselves. That's why I get:

In[79]:= Evaluate[Alphabet[]] =.

During evaluation of In[79]:= Unset::usraw: Cannot unset raw object a. >>

During evaluation of In[79]:= Unset::usraw: Cannot unset raw object b. >>

During evaluation of In[79]:= Unset::usraw: Cannot unset raw object c. >>

During evaluation of In[79]:= General::stop: Further output of Unset::usraw will be suppressed during this calculation. >>

Out[79]= { $Failed, $Failed, $Failed, $Failed, $Failed, $Failed, \ $Failed, $Failed, $Failed, $Failed, $Failed, $Failed, $Failed, \ $Failed, $Failed, $Failed, $Failed, $Failed, $Failed, $Failed, \ $Failed, $Failed, $Failed, $Failed, $Failed, $Failed}

Is there a way to make variables stay symbolic for a specific calculation, even if I assigned a value to it? Are there any other ways to achieve what I am trying to do?

Thanks again!

POSTED BY: Florian Wicher
Posted 9 years ago

For some background, the individual letters in the Alphabet[] function are strings. A string cannot be set equal to another object, that is why Gianluca made use of the ToExpression command.

If you typed out {a,b,c...} = Range[26] it would work for the individual letters, or as Gianluca did, the ToExpression command accomplishes this.

POSTED BY: Kyle Martin

Try with

Evaluate[ToExpression /@ Alphabet[]] = Range[26]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract