Re: list referencing with [[]][[]]
- To: mathgroup at smc.vnet.net
- Subject: [mg13993] Re: list referencing with [[]][[]]
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Sat, 12 Sep 1998 16:59:17 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Tim Dellinger wrote:
>
> According to "the book" section 1.8.4,
>
> t[[1]][[2]] "is equivalent to" t[[1,2]], "but is clumsier to write"
>
> and that is all that I can find concerning the difference between the
> two ways of refering to a list element.
>
> But...
>
> In[37]:=
> alist = {{1,2},{3,4}}; alist[[2,1]] = 7 ; alist
>
> Out[37]=
> {{1,2},{7,4}}
>
> So far so good, and yet...
>
> In[39]:=
> anotherlist = {{1,2},{3,4}}; anotherlist[[2]][[1]] = 7
> Set::"setps":
> "\!\(anotherlist \\[LeftDoubleBracket] 2 \\[RightDoubleBracket]\) in
> \
> assignment of part is not a symbol."
> Out[39]=
> 7
>
> What's the problem here? Is there a difference between the two ways of
> referencing list elements that "the book" neglects to mention?
Tim:
t[[1]][[2]] is equivalent to t[[1,2]] for extraction of parts, as
indicated by your example.
But for part resetting, as in your second example, it in not equivalent.
The catch is that part resetting like
a[[3]] = 7
works only when
1) a is a symbol that has been assigned an expression expr with
a = expr
(a:= expr will not do) 2) expr has a part 3
During evaluation,
anotherlist[[2]][[1]] = 7
(that is (anotherlist[[2]])[[1]] = 7 ) becomes
{3,4} [[1]] = 7
which does not meet requirment 1).
Allan
--
Allan Hayes
Training and Consulting
Leicester, UK
hay at haystack.demon.co.uk
http://www.haystack.demon.co.uk
voice: +44 (0)116 271 4198
fax: +44 (0)116 271 8642