[Date Index]
[Thread Index]
[Author Index]
Re: Increment and AddTo
*To*: mathgroup at smc.vnet.net
*Subject*: [mg64568] Re: [mg64551] Increment and AddTo
*From*: "Erickson Paul-CPTP18" <Paul.Erickson at Motorola.com>
*Date*: Thu, 23 Feb 2006 00:34:04 -0500 (EST)
*Sender*: owner-wri-mathgroup at wolfram.com
Well I think I can explain it, but ...
++i is executed twice as indicated below in bold-red. The first time as
part of determining the value of the index to find the value to be added
to 5 (since pre-fix, I think this is correct) and then a second time as
part of the expression to find where the sum should be assigned to in
the indexed string (I'd assume this execution is a surprise). Therefore,
it is effectively start i=0, autoincrement i, find the v[i] or v[1], add
to 5, now to assign back first autoincrement i, then assign 6 back to
v[2].
Paul
Using trace.
Clear [ i, v ]
Trace[
i = 0;
v = {1, 2, 3};
v[[++i]] += 5;
v;
i
]
{i=0;v={1,2,3};vP++iT+=5;v;i,{i=0,0},{v={1,2,3},{1,2,3}},{vP++iT+=5,{{v,
{1,2,3}},{++i,{i,0},{i=1,1},1},{1,2,3}P1T,1},{{1+5,6},vP++iT=6,6},6},{v,
{1,6,3}},{i,2},2}
-----Original Message-----
From: dh [mailto:dh at metrohm.ch]
To: mathgroup at smc.vnet.net
Subject: [mg64568] [mg64551] Increment and AddTo
Hello,
can anybody explain the following behaviour:
$Version = 5.1 for Microsoft Windows (October 25, 2004)
i = 0;
v = {1, 2, 3};
v[[++i]] += 10;
v
i
gives
{1,11,3}
2
i has been incremented by 2!
i++ is even more interesting:
i = 1;
v = {1, 2, 3};
v[[i++]] += 10;
v
i
gives:
{1,11,3}
2
again i is incremented by 2, but for the sum v[[1]] is used and stored
in v[[2]]
Further:
i = 0;
v = {1, 2, 3};
++v[[++i]] ;
v
i
gives:
{1,2,3}
2
Again i has been incremented by 2, but v has not been touched at all.
Daniel
Prev by Date:
**Re: Also force AxesOrigin to be in the lower right corner?**
Next by Date:
**Re: Counting the number of paths in a network**
Previous by thread:
**Re: Increment and AddTo**
Next by thread:
**Import Filemaker Pro 7 database into Mathematica 5.2**
| |