Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2002
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Assigning to a sublist

  • To: mathgroup at smc.vnet.net
  • Subject: [mg35175] RE: [mg35161] Assigning to a sublist
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Fri, 28 Jun 2002 02:31:08 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

> -----Original Message-----
> From: Wolf, Hartmut 
To: mathgroup at smc.vnet.net
> Sent: Thursday, June 27, 2002 9:46 AM
> Subject: [mg35175] RE: [mg35161] Assigning to a sublist
> 
> 
> 
> > -----Original Message-----
> > From: Bob Harris [mailto:nitlion at mindspring.com]
To: mathgroup at smc.vnet.net
> > Sent: Thursday, June 27, 2002 6:24 AM
> > To: mathgroup at smc.vnet.net
> > Subject: [mg35175] [mg35161] Assigning to a sublist
> > 
> > 
> > Howdy,
> > 
> > I'm trying to assign a new value to an entry in a sublist (of 
> > another list),
> > and I can't understand why it won't work.
> > 
> > For example, I do the following:
> > 
> >   In[1]:= zz = {{2, {{1, 1}}}, {10, {{3, 1}}}, {5, {{2, 1}}}}
> >   Out[1]= {{2, {{1, 1}}}, {10, {{3, 1}}}, {5, {{2, 1}}}}
> > 
> >   In[2]:= zz[[3]][[2]]
> >   Out[2]= {{2, 1}}
> > 
> >   In[3]:= Append[zz[[3]][[2]], {1, 1, 1, 1, 1}]
> >   Out[3]= {{2, 1}, {1, 1, 1, 1, 1}}
> > 
> >   In[4]:= zz[[3]][[2]] = Append[zz[[3]][[2]], {1, 1, 1, 1, 1}]
> >   Set::"setps : zz[[3]] in assignment of part is not a symbol."
> >   Out[4]= {{2, 1}, {1, 1, 1, 1, 1}}
> > 
> > For some reason it doesn't like the assignment.  What 
> > confuses me is that is
> > zz[[3]][[2]] were just a variable, it would work.  Further, 
> > if it were just
> > an entry at the *top* level of a list, it would work, as this 
> > example shows:
> > 
> >   In[5]:= yy = zz[[3]]
> >   Out[5]= {5, {{2, 1}}}
> > 
> >   In[6]:= yy[[2]]
> >   Out[6]= {{2, 1}}
> > 
> >   In[7]:= yy[[2]] = Append[yy[[2]], {1, 1, 1, 1, 1}]
> >   Out[7]= {{2, 1}, {1, 1, 1, 1, 1}}
> > 
> > So it seems like the issue is just that deeply nested things 
> > don't behave
> > like things that are not as deeply nested.  Am I right about 
> > that?  How can
> > I modify an entry in a sublist?
> > 
> > Thanks,
> > Bob H
> > 
> > 
> 
> Bob,
> 
> observe
> 
> In[1]:= zz = {{2, {{1, 1}}}, {10, {{3, 1}}}, {5, {{2, 1}}}}
> Out[1]= {{2, {{1, 1}}}, {10, {{3, 1}}}, {5, {{2, 1}}}}
> 
> In[2]:= zz[[3, 2]] = Append[zz[[3]][[2]], {1, 1, 1, 1, 1}]
> Out[2]= {{2, 1}, {1, 1, 1, 1, 1}}
> 
> In[3]:= zz
> Out[3]= 
> {{2, {{1, 1}}}, {10, {{3, 1}}}, {5, {{2, 1}, {1, 1, 1, 1, 1}}}}
> 
> So here at the lhs of Set zz[[3]][[2]] is not the same as zz[[3, 2]]
> 
> In[4]:= Hold[zz[[3]][[2]]] // FullForm
> Out[4]//FullForm= Hold[Part[Part[zz, 3], 2]]
> 
> In[5]:= Hold[zz[[3, 2]]] // FullForm
> Out[5]//FullForm= Hold[Part[zz, 3, 2]]
> 
> Set looks at the first element of Part at the (unevaluated) 
> lhs, this must be a symbol, and zz[[3]] is none, as the error 
> message tells. Set does not look down further, clearly on 
> reasons of performance. So just use simple (flat) Part.
> 
> --
> Hartmut 
> 

An additional remark: Although not needed in any way, (and let's forget
Function, With, Replace etc. for a while) it might be an interesting
question how to flatten Part at the lhs of Set. I found this way:

 
In[62]:= zz = {{2, {{1, 1}}}, {10, {{3, 1}}}, {5, {{2, 1}}}}
Out[62]= {{2, {{1, 1}}}, {10, {{3, 1}}}, {5, {{2, 1}}}}

In[63]:= Block[{Set, Part, Append, zz}, 
  Flatten[zz[[3]][[2]], 2, Part] = Append[zz[[3]][[2]], {1, 1, 1, 1, 1}]]
Out[63]=
{{2, 1}, {1, 1, 1, 1, 1}}

In[64]:= zz
Out[64]=
{{2, {{1, 1}}}, {10, {{3, 1}}}, {5, {{2, 1}, {1, 1, 1, 1, 1}}}}

--
Hartmut



  • Prev by Date: RE: Replacement question
  • Next by Date: Re: Replacement question
  • Previous by thread: RE: Assigning to a sublist
  • Next by thread: Tom Wickham Jones presents webMathematica course in Amsterdam