Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have found that it is possible to assign variables from lists, as follows.

In[1]:= r = {1, 2, 3};
In[2]:= {a, b, c} = r;
In[3]:= b
Out[3]= 2

However, if I try to do this within a With or Module statement, it does not evaluate:

In[4]:= With[{{x, y, z} = r}, x]
Out[4]= With[{{x, y, z} = r}, x]
In[5]:= Module[{{x, y, z} = r}, x]
Out[5]= Module[{{x, y, z} = r}, x]

whereas I expected it to work as:

In[6]:= With[{x = r[[1]], y = r[[2]], z = r[[3]]}, y]
Out[6]= 2
In[7]:= Module[{x = r[[1]], y = r[[2]], z = r[[3]]}, y]
Out[7]= 2

or even:

In[8]:= Module[{x, y, z}, {x, y, z} = r; y]
Out[8]= 2

Why is this? Is there an accepted way to do this?

share|improve this question
2  
related question –  Kuba Aug 21 '13 at 12:54
1  
Exactly this question was asked on SO some time ago. –  Leonid Shifrin Aug 21 '13 at 13:03
    
Leonid's linked question is exactly what I was asking, with excellent responses. I've tried to flag this as a duplicate, but as it's not a Mathematica SE page I'm unable to. –  Gyppo Aug 21 '13 at 13:19
    
@Gyppo This is not really a dupe here, since SO is a separate site. This leaves some room for further answers, possibly better than those in that SO question, unless that question gets migrated from SO. I would personally add / change some things in my answer there, but no time for that right now. –  Leonid Shifrin Aug 21 '13 at 13:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.